parser.rs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{
  19. borrow::Borrow, collections::HashMap, hash::Hash, io::Result, iter::Peekable, str::Chars,
  20. };
  21. use super::{
  22. ast::{Arg, Constant, Literal, Statement, StatementType, Variable, Witness},
  23. constants::{ALLOWED_FIELDS, MAX_K, MAX_NS_LEN},
  24. error::ErrorEmitter,
  25. lexer::{Token, TokenType},
  26. LitType, Opcode, VarType,
  27. };
  28. /// zkas language builtin keywords.
  29. /// These can not be used anywhere except where they are expected.
  30. const KEYWORDS: [&str; 5] = ["k", "field", "constant", "witness", "circuit"];
  31. /// Forbidden namespaces
  32. const NOPE_NS: [&str; 4] = [".constant", ".literal", ".witness", ".circuit"];
  33. /// Valid EcFixedPoint constant names supported by the VM.
  34. const VALID_ECFIXEDPOINT: [&str; 1] = ["VALUE_COMMIT_RANDOM"];
  35. /// Valid EcFixedPointShort constant names supported by the VM.
  36. const VALID_ECFIXEDPOINTSHORT: [&str; 1] = ["VALUE_COMMIT_VALUE"];
  37. /// Valid EcFixedPointBase constant names supported by the VM.
  38. const VALID_ECFIXEDPOINTBASE: [&str; 2] = ["VALUE_COMMIT_RANDOM_BASE", "NULLIFIER_K"];
  39. #[derive(Clone)]
  40. struct IndexMap<K, V> {
  41. pub order: Vec<K>,
  42. pub map: HashMap<K, V>,
  43. }
  44. impl<K, V> IndexMap<K, V> {
  45. fn new() -> Self {
  46. Self { order: vec![], map: HashMap::new() }
  47. }
  48. }
  49. impl<K, V> IndexMap<K, V>
  50. where
  51. K: Eq + Hash + Send + Sync + Clone + 'static,
  52. V: Send + Sync + Clone + 'static,
  53. {
  54. fn contains_key<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> bool
  55. where
  56. K: Borrow<Q>,
  57. {
  58. self.map.contains_key(k)
  59. }
  60. fn get<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> Option<&V>
  61. where
  62. K: Borrow<Q>,
  63. {
  64. self.map.get(k)
  65. }
  66. fn insert(&mut self, k: K, v: V) -> Option<V> {
  67. self.order.push(k.clone());
  68. self.map.insert(k, v)
  69. }
  70. fn scam_iter(&self) -> Vec<(K, V)> {
  71. self.order.iter().map(|k| (k.clone(), self.get(k).unwrap().clone())).collect()
  72. }
  73. }
  74. pub struct Parser {
  75. tokens: Vec<Token>,
  76. error: ErrorEmitter,
  77. }
  78. type Parsed = (String, u32, Vec<Constant>, Vec<Witness>, Vec<Statement>);
  79. impl Parser {
  80. pub fn new(filename: &str, source: Chars, tokens: Vec<Token>) -> Self {
  81. // For nice error reporting, we'll load everything into a string
  82. // vector so we have references to lines.
  83. let lines: Vec<String> = source.as_str().lines().map(|x| x.to_string()).collect();
  84. let error = ErrorEmitter::new("Parser", filename, lines);
  85. Self { tokens, error }
  86. }
  87. pub fn parse(&self) -> Result<Parsed> {
  88. // We use these to keep state while parsing.
  89. let mut namespace = None;
  90. let (mut declaring_constant, mut declared_constant) = (false, false);
  91. let (mut declaring_witness, mut declared_witness) = (false, false);
  92. let (mut declaring_circuit, mut declared_circuit) = (false, false);
  93. // The tokens gathered from each of the sections
  94. let mut constant_tokens = vec![];
  95. let mut witness_tokens = vec![];
  96. let mut circuit_tokens = vec![];
  97. // Tokens belonging to the current statement
  98. let mut circuit_stmt = vec![];
  99. // All completed statements are pushed here
  100. let mut circuit_stmts = vec![];
  101. // Contains constant and witness sections
  102. let mut ast_inner = IndexMap::new();
  103. let mut ast = IndexMap::new();
  104. if self.tokens.is_empty() {
  105. return Err(self.error.abort("Source file does not contain any valid tokens.", 0, 0))
  106. }
  107. if self.tokens[0].token_type != TokenType::Symbol {
  108. return Err(self.error.abort(
  109. "Source file does not start with a section. Expected `constant/witness/circuit`.",
  110. 0,
  111. 0,
  112. ))
  113. }
  114. let mut iter = self.tokens.iter();
  115. // The first thing that has to be declared in the source
  116. // code is the constant "k" which defines 2^k rows that
  117. // the circuit needs to successfully execute.
  118. let Some((k, equal, number, semicolon)) = NextTuple4::next_tuple(&mut iter) else {
  119. return Err(self.error.abort("Source file does not start with k=n;", 0, 0))
  120. };
  121. if k.token_type != TokenType::Symbol ||
  122. equal.token_type != TokenType::Assign ||
  123. number.token_type != TokenType::Number ||
  124. semicolon.token_type != TokenType::Semicolon
  125. {
  126. return Err(self.error.abort("Source file does not start with k=n;", k.line, k.column))
  127. }
  128. if k.token != "k" {
  129. return Err(self.error.abort("Source file does not start with k=n;", k.line, k.column))
  130. }
  131. // Ensure that the value for k can be parsed correctly into the token type.
  132. // The below code catches cases where a large k value exceeding the bounds of the target
  133. // type is supplied by the user. Without this check an integer overflow can occur.
  134. let declared_k = match number.token.parse() {
  135. Ok(v) => v,
  136. Err(e) => {
  137. return Err(self.error.abort(
  138. &format!("k param is invalid, max allowed is {MAX_K}. Error: {e}"),
  139. number.line,
  140. number.column,
  141. ))
  142. }
  143. };
  144. if declared_k > MAX_K {
  145. return Err(self.error.abort(
  146. &format!("k param is too high, max allowed is {MAX_K}"),
  147. number.line,
  148. number.column,
  149. ))
  150. }
  151. // Then we declare the field we're working in.
  152. let Some((field, equal, field_name, semicolon)) = NextTuple4::next_tuple(&mut iter) else {
  153. return Err(self.error.abort(
  154. "Source file does not declare field after k",
  155. k.line,
  156. k.column,
  157. ))
  158. };
  159. if field.token_type != TokenType::Symbol ||
  160. equal.token_type != TokenType::Assign ||
  161. field_name.token_type != TokenType::String ||
  162. semicolon.token_type != TokenType::Semicolon
  163. {
  164. return Err(self.error.abort(
  165. "Source file does not declare field after k",
  166. field.line,
  167. field.column,
  168. ))
  169. }
  170. if field.token != "field" {
  171. return Err(self.error.abort(
  172. "Source file does not declare field after k",
  173. field.line,
  174. field.column,
  175. ))
  176. }
  177. if !ALLOWED_FIELDS.contains(&field_name.token.as_str()) {
  178. return Err(self.error.abort(
  179. &format!(
  180. "Declared field \"{}\" is not supported. Use any of: {ALLOWED_FIELDS:?}",
  181. field_name.token
  182. ),
  183. field_name.line,
  184. field_name.column,
  185. ))
  186. }
  187. while let Some(t) = iter.next() {
  188. // Sections "constant", "witness", and "circuit" are
  189. // the sections we must be declaring in our source code.
  190. // When we find one, we'll take all the tokens found in
  191. // the section and place them in their respective vec.
  192. // NOTE: Currently this logic depends on the fact that
  193. // the sections are closed off with braces. This should
  194. // be revisited later when we decide to add other lang
  195. // functionality that also depends on using braces.
  196. if !declaring_constant && !declaring_witness && !declaring_circuit {
  197. //
  198. // We use this macro to avoid code repetition in the following
  199. // match statement for soaking up the section tokens.
  200. macro_rules! absorb_inner_tokens {
  201. ($v:ident) => {
  202. for inner in iter.by_ref() {
  203. if KEYWORDS.contains(&inner.token.as_str()) &&
  204. inner.token_type == TokenType::Symbol
  205. {
  206. return Err(self.error.abort(
  207. &format!("Keyword '{}' used in improper place.", inner.token),
  208. inner.line,
  209. inner.column,
  210. ))
  211. }
  212. $v.push(inner.clone());
  213. if inner.token_type == TokenType::RightBrace {
  214. break
  215. }
  216. }
  217. };
  218. }
  219. match t.token.as_str() {
  220. "constant" => {
  221. declaring_constant = true;
  222. absorb_inner_tokens!(constant_tokens);
  223. }
  224. "witness" => {
  225. declaring_witness = true;
  226. absorb_inner_tokens!(witness_tokens);
  227. }
  228. "circuit" => {
  229. declaring_circuit = true;
  230. absorb_inner_tokens!(circuit_tokens);
  231. }
  232. x => {
  233. return Err(self.error.abort(
  234. &format!("Section `{x}` is not a valid section"),
  235. t.line,
  236. t.column,
  237. ))
  238. }
  239. }
  240. }
  241. // We use this macro to set or check that the namespace of all sections
  242. // is the same and no stray strings appeared.
  243. macro_rules! check_namespace {
  244. ($t:ident) => {
  245. if let Some(ns) = namespace.clone() {
  246. if ns != $t[0].token {
  247. return Err(self.error.abort(
  248. &format!("Found '{}' namespace, expected '{ns}'.", $t[0].token),
  249. $t[0].line,
  250. $t[0].column,
  251. ))
  252. }
  253. } else {
  254. if NOPE_NS.contains(&$t[0].token.as_str()) {
  255. return Err(self.error.abort(
  256. &format!("'{}' cannot be a namespace.", $t[0].token),
  257. $t[0].line,
  258. $t[0].column,
  259. ))
  260. }
  261. namespace = Some($t[0].token.clone());
  262. if namespace.as_ref().unwrap().as_bytes().len() > MAX_NS_LEN {
  263. return Err(self.error.abort(
  264. &format!("Namespace too long, max {MAX_NS_LEN} bytes"),
  265. $t[0].line,
  266. $t[0].column,
  267. ))
  268. }
  269. }
  270. };
  271. }
  272. // Parse the constant section into the AST.
  273. if declaring_constant {
  274. if declared_constant {
  275. return Err(self.error.abort(
  276. "Duplicate `constant` section found.",
  277. t.line,
  278. t.column,
  279. ))
  280. }
  281. self.check_section_structure("constant", constant_tokens.clone())?;
  282. check_namespace!(constant_tokens);
  283. let mut constants_map = IndexMap::new();
  284. // This is everything between the braces: { ... }
  285. let mut constant_inner = constant_tokens[2..constant_tokens.len() - 1].iter();
  286. while let Some((typ, name, comma)) = NextTuple3::next_tuple(&mut constant_inner) {
  287. if comma.token_type != TokenType::Comma {
  288. return Err(self.error.abort(
  289. "Separator is not a comma.",
  290. comma.line,
  291. comma.column,
  292. ))
  293. }
  294. // No variable shadowing
  295. if constants_map.contains_key(name.token.as_str()) {
  296. return Err(self.error.abort(
  297. &format!(
  298. "Section `constant` already contains the token `{}`.",
  299. &name.token
  300. ),
  301. name.line,
  302. name.column,
  303. ))
  304. }
  305. constants_map.insert(name.token.clone(), (name.clone(), typ.clone()));
  306. }
  307. if constant_inner.next().is_some() {
  308. return Err(self.error.abort(
  309. "Internal error, leftovers in 'constant' iterator",
  310. 0,
  311. 0,
  312. ))
  313. }
  314. ast_inner.insert("constant".to_string(), constants_map);
  315. declaring_constant = false;
  316. declared_constant = true;
  317. }
  318. // Parse the witness section into the AST.
  319. if declaring_witness {
  320. if declared_witness {
  321. return Err(self.error.abort(
  322. "Duplicate `witness` section found.",
  323. t.line,
  324. t.column,
  325. ))
  326. }
  327. self.check_section_structure("witness", witness_tokens.clone())?;
  328. check_namespace!(witness_tokens);
  329. let mut witnesses_map = IndexMap::new();
  330. // This is everything between the braces: { ... }
  331. let mut witness_inner = witness_tokens[2..witness_tokens.len() - 1].iter();
  332. while let Some((typ, name, comma)) = NextTuple3::next_tuple(&mut witness_inner) {
  333. if comma.token_type != TokenType::Comma {
  334. return Err(self.error.abort(
  335. "Separator is not a comma.",
  336. comma.line,
  337. comma.column,
  338. ))
  339. }
  340. // No variable shadowing
  341. if witnesses_map.contains_key(name.token.as_str()) {
  342. return Err(self.error.abort(
  343. &format!(
  344. "Section `witness` already contains the token `{}`.",
  345. &name.token
  346. ),
  347. name.line,
  348. name.column,
  349. ))
  350. }
  351. witnesses_map.insert(name.token.clone(), (name.clone(), typ.clone()));
  352. }
  353. if witness_inner.next().is_some() {
  354. return Err(self.error.abort(
  355. "Internal error, leftovers in 'witness' iterator",
  356. 0,
  357. 0,
  358. ))
  359. }
  360. ast_inner.insert("witness".to_string(), witnesses_map);
  361. declaring_witness = false;
  362. declared_witness = true;
  363. }
  364. // Parse the circuit section into the AST.
  365. if declaring_circuit {
  366. if declared_circuit {
  367. return Err(self.error.abort(
  368. "Duplicate `circuit` section found.",
  369. t.line,
  370. t.column,
  371. ))
  372. }
  373. self.check_section_structure("circuit", circuit_tokens.clone())?;
  374. check_namespace!(circuit_tokens);
  375. // Grab tokens for each statement
  376. for i in circuit_tokens[2..circuit_tokens.len() - 1].iter() {
  377. if i.token_type == TokenType::Semicolon {
  378. // Push completed statement to the heap
  379. circuit_stmts.push(circuit_stmt.clone());
  380. circuit_stmt = vec![];
  381. continue
  382. }
  383. circuit_stmt.push(i.clone());
  384. }
  385. declaring_circuit = false;
  386. declared_circuit = true;
  387. }
  388. }
  389. // Tokens have been processed and ast is complete
  390. let ns = match namespace {
  391. Some(v) => v,
  392. None => return Err(self.error.abort("Missing namespace in .zk source.", 0, 0)),
  393. };
  394. ast.insert(ns.clone(), ast_inner);
  395. let constants = {
  396. let c = match ast.get(&ns).unwrap().get("constant") {
  397. Some(c) => c,
  398. None => {
  399. return Err(self.error.abort("Missing `constant` section in .zk source.", 0, 0))
  400. }
  401. };
  402. self.parse_ast_constants(c)?
  403. };
  404. let witnesses = {
  405. let c = match ast.get(&ns).unwrap().get("witness") {
  406. Some(c) => c,
  407. None => {
  408. return Err(self.error.abort("Missing `witness` section in .zk source.", 0, 0))
  409. }
  410. };
  411. self.parse_ast_witness(c)?
  412. };
  413. let statements = self.parse_ast_circuit(circuit_stmts)?;
  414. if statements.is_empty() {
  415. return Err(self.error.abort("Circuit section is empty.", 0, 0))
  416. }
  417. Ok((ns, declared_k, constants, witnesses, statements))
  418. }
  419. /// Routine checks on section structure
  420. fn check_section_structure(&self, section: &str, tokens: Vec<Token>) -> Result<()> {
  421. // Offsets 0 and 1 are accessed directly below, so we need a length of at
  422. // least 2 in order to avoid an index-out-of-bounds panic.
  423. if tokens.len() < 2 {
  424. return Err(self.error.abort("Insufficient number of tokens in section.", 0, 0))
  425. }
  426. if tokens[0].token_type != TokenType::String {
  427. return Err(self.error.abort(
  428. "Section declaration must start with a naming string.",
  429. tokens[0].line,
  430. tokens[0].column,
  431. ))
  432. }
  433. if tokens[1].token_type != TokenType::LeftBrace {
  434. return Err(self.error.abort(
  435. "Section must be opened with a left brace '{'",
  436. tokens[0].line,
  437. tokens[0].column,
  438. ))
  439. }
  440. if tokens.last().unwrap().token_type != TokenType::RightBrace {
  441. return Err(self.error.abort(
  442. "Section must be closed with a right brace '}'",
  443. tokens[0].line,
  444. tokens[0].column,
  445. ))
  446. }
  447. match section {
  448. "constant" | "witness" => {
  449. if tokens.len() == 3 {
  450. self.error.warn(&format!("{section} section is empty."), 0, 0);
  451. }
  452. if tokens[2..tokens.len() - 1].len() % 3 != 0 {
  453. return Err(self.error.abort(
  454. &format!("Invalid number of elements in '{section}' section. Must be pairs of '<Type> <name>' separated with a comma ','."),
  455. tokens[0].line,
  456. tokens[0].column
  457. ))
  458. }
  459. }
  460. "circuit" => {
  461. if tokens.len() == 3 {
  462. return Err(self.error.abort("circuit section is empty.", 0, 0))
  463. }
  464. if tokens[tokens.len() - 2].token_type != TokenType::Semicolon {
  465. return Err(self.error.abort(
  466. "Circuit section does not end with a semicolon. Would never finish parsing.",
  467. tokens[tokens.len()-2].line,
  468. tokens[tokens.len()-2].column,
  469. ))
  470. }
  471. }
  472. _ => unreachable!(),
  473. };
  474. Ok(())
  475. }
  476. fn parse_ast_constants(&self, ast: &IndexMap<String, (Token, Token)>) -> Result<Vec<Constant>> {
  477. let mut ret = vec![];
  478. // k = name
  479. // v = (name, type)
  480. for (k, v) in ast.scam_iter() {
  481. if v.0.token != k {
  482. return Err(self.error.abort(
  483. &format!("Constant name `{}` doesn't match token `{k}`.", v.0.token),
  484. v.0.line,
  485. v.0.column,
  486. ))
  487. }
  488. if v.0.token_type != TokenType::Symbol {
  489. return Err(self.error.abort(
  490. &format!("Constant name `{}` is not a symbol.", v.0.token),
  491. v.0.line,
  492. v.0.column,
  493. ))
  494. }
  495. if v.1.token_type != TokenType::Symbol {
  496. return Err(self.error.abort(
  497. &format!("Constant type `{}` is not a symbol.", v.1.token),
  498. v.1.line,
  499. v.1.column,
  500. ))
  501. }
  502. // Valid constant types, these are the constants/generators supported
  503. // in `src/crypto/constants.rs` and `src/crypto/constants/`.
  504. match v.1.token.as_str() {
  505. "EcFixedPoint" => {
  506. if !VALID_ECFIXEDPOINT.contains(&v.0.token.as_str()) {
  507. return Err(self.error.abort(
  508. &format!(
  509. "`{}` is not a valid EcFixedPoint constant. Supported: {VALID_ECFIXEDPOINT:?}",
  510. v.0.token.as_str()
  511. ),
  512. v.0.line,
  513. v.0.column,
  514. ))
  515. }
  516. ret.push(Constant {
  517. name: k.to_string(),
  518. typ: VarType::EcFixedPoint,
  519. line: v.1.line,
  520. column: v.1.column,
  521. });
  522. }
  523. "EcFixedPointShort" => {
  524. if !VALID_ECFIXEDPOINTSHORT.contains(&v.0.token.as_str()) {
  525. return Err(self.error.abort(
  526. &format!(
  527. "`{}` is not a valid EcFixedPointShort constant. Supported: {VALID_ECFIXEDPOINTSHORT:?}",
  528. v.0.token.as_str()
  529. ),
  530. v.0.line,
  531. v.0.column,
  532. ))
  533. }
  534. ret.push(Constant {
  535. name: k.to_string(),
  536. typ: VarType::EcFixedPointShort,
  537. line: v.1.line,
  538. column: v.1.column,
  539. });
  540. }
  541. "EcFixedPointBase" => {
  542. if !VALID_ECFIXEDPOINTBASE.contains(&v.0.token.as_str()) {
  543. return Err(self.error.abort(
  544. &format!(
  545. "`{}` is not a valid EcFixedPointBase constant. Supported: {VALID_ECFIXEDPOINTBASE:?}",
  546. v.0.token.as_str()
  547. ),
  548. v.0.line,
  549. v.0.column,
  550. ))
  551. }
  552. ret.push(Constant {
  553. name: k.to_string(),
  554. typ: VarType::EcFixedPointBase,
  555. line: v.1.line,
  556. column: v.1.column,
  557. });
  558. }
  559. x => {
  560. return Err(self.error.abort(
  561. &format!("`{x}` is an unsupported constant type."),
  562. v.1.line,
  563. v.1.column,
  564. ))
  565. }
  566. }
  567. }
  568. Ok(ret)
  569. }
  570. fn parse_ast_witness(&self, ast: &IndexMap<String, (Token, Token)>) -> Result<Vec<Witness>> {
  571. let mut ret = vec![];
  572. // k = name
  573. // v = (name, type)
  574. for (k, v) in ast.scam_iter() {
  575. if v.0.token != k {
  576. return Err(self.error.abort(
  577. &format!("Witness name `{}` doesn't match token `{k}`.", v.0.token),
  578. v.0.line,
  579. v.0.column,
  580. ))
  581. }
  582. if v.0.token_type != TokenType::Symbol {
  583. return Err(self.error.abort(
  584. &format!("Witness name `{}` is not a symbol.", v.0.token),
  585. v.0.line,
  586. v.0.column,
  587. ))
  588. }
  589. if v.1.token_type != TokenType::Symbol {
  590. return Err(self.error.abort(
  591. &format!("Witness type `{}` is not a symbol.", v.1.token),
  592. v.1.line,
  593. v.1.column,
  594. ))
  595. }
  596. // Valid witness types
  597. // TODO: change to TryFrom impl for VarType
  598. match v.1.token.as_str() {
  599. "EcPoint" => {
  600. ret.push(Witness {
  601. name: k.to_string(),
  602. typ: VarType::EcPoint,
  603. line: v.0.line,
  604. column: v.0.column,
  605. });
  606. }
  607. "EcNiPoint" => {
  608. ret.push(Witness {
  609. name: k.to_string(),
  610. typ: VarType::EcNiPoint,
  611. line: v.0.line,
  612. column: v.0.column,
  613. });
  614. }
  615. "Base" => {
  616. ret.push(Witness {
  617. name: k.to_string(),
  618. typ: VarType::Base,
  619. line: v.0.line,
  620. column: v.0.column,
  621. });
  622. }
  623. "Scalar" => {
  624. ret.push(Witness {
  625. name: k.to_string(),
  626. typ: VarType::Scalar,
  627. line: v.0.line,
  628. column: v.0.column,
  629. });
  630. }
  631. "MerklePath" => {
  632. ret.push(Witness {
  633. name: k.to_string(),
  634. typ: VarType::MerklePath,
  635. line: v.0.line,
  636. column: v.0.column,
  637. });
  638. }
  639. "SparseMerklePath" => {
  640. ret.push(Witness {
  641. name: k.to_string(),
  642. typ: VarType::SparseMerklePath,
  643. line: v.0.line,
  644. column: v.0.column,
  645. });
  646. }
  647. "Uint32" => {
  648. ret.push(Witness {
  649. name: k.to_string(),
  650. typ: VarType::Uint32,
  651. line: v.0.line,
  652. column: v.0.column,
  653. });
  654. }
  655. "Uint64" => {
  656. ret.push(Witness {
  657. name: k.to_string(),
  658. typ: VarType::Uint64,
  659. line: v.0.line,
  660. column: v.0.column,
  661. });
  662. }
  663. x => {
  664. return Err(self.error.abort(
  665. &format!("`{x}` is an unsupported witness type."),
  666. v.1.line,
  667. v.1.column,
  668. ))
  669. }
  670. }
  671. }
  672. Ok(ret)
  673. }
  674. fn parse_ast_circuit(&self, statements: Vec<Vec<Token>>) -> Result<Vec<Statement>> {
  675. // The statement layouts/syntax in the language are as follows:
  676. //
  677. // C = poseidon_hash(pub_x, pub_y, value, token, serial);
  678. // | | | | |
  679. // V V V V V
  680. // variable opcode arg arg
  681. // assign
  682. //
  683. // constrain_instance(C);
  684. // | |
  685. // V V
  686. // opcode arg
  687. //
  688. // inner opcode arg
  689. // |
  690. // constrain_instance(ec_get_x(foo));
  691. // | |
  692. // V V
  693. // opcode arg as opcode
  694. //
  695. // In the latter, we want to support nested function calls, e.g.:
  696. //
  697. // constrain_instance(ec_get_x(token_commit));
  698. //
  699. // The inner call's result would still get pushed on the heap,
  700. // but it will not be accessible in any other scope.
  701. //
  702. // In certain opcodes, we also support literal types, and the
  703. // opcodes can return a variable type after running the operation.
  704. // e.g.
  705. // one = witness_base(1);
  706. // zero = witness_base(0);
  707. //
  708. // The literal type is used only in the function call's scope, but
  709. // the result is then accessible on the heap to be used by further
  710. // computation.
  711. //
  712. // Regarding multiple return values from opcodes, this is perhaps
  713. // not necessary for the current language scope, as this is a low
  714. // level representation. Note that it could be relatively easy to
  715. // modify the parsing logic to support that here. For now we'll
  716. // defer it, and if at some point we decide that the language is
  717. // too expressive and noisy, we'll consider having multiple return
  718. // types. It also very much depends on the type of functions/opcodes
  719. // that we want to support.
  720. // Vec of statements to return from this entire parsing operation.
  721. let mut ret = vec![];
  722. // Here, our statements tokens have been parsed and delimited by
  723. // semicolons (;) in the source file. This iterator contains each
  724. // of those statements as an array of tokens we then consume and
  725. // build the AST further.
  726. for statement in statements {
  727. if statement.is_empty() {
  728. continue
  729. }
  730. let (mut left_paren, mut right_paren, mut left_bracket, mut right_bracket) =
  731. (0, 0, 0, 0);
  732. for i in &statement {
  733. match i.token.as_str() {
  734. "(" => left_paren += 1,
  735. ")" => right_paren += 1,
  736. "[" => left_bracket += 1,
  737. "]" => right_bracket += 1,
  738. _ => {}
  739. }
  740. }
  741. if (left_paren == 0 && right_paren == 0) && (left_bracket == 0 && right_bracket == 0) {
  742. return Err(self.error.abort(
  743. "Statement must include a function call or array initialization. No parentheses or square brackets present.",
  744. statement[0].line,
  745. statement[0].column,
  746. ))
  747. }
  748. if (left_bracket != right_bracket) || (left_paren != right_paren) {
  749. return Err(self.error.abort(
  750. "Parentheses or brackets are not matched.",
  751. statement[0].line,
  752. statement[0].column,
  753. ))
  754. }
  755. // Is there a valid use-case for defining nested arrays? For now,
  756. // if square brackets are present, raise an error unless there is
  757. // exactly one pair.
  758. if left_bracket > 1 {
  759. return Err(self.error.abort(
  760. "Only one pair of brackets allowed for array declaration",
  761. statement[0].line,
  762. statement[0].column,
  763. ))
  764. }
  765. // Peekable iterator so we can see tokens in advance
  766. // without consuming the iterator.
  767. let mut iter = statement.iter().peekable();
  768. // Dummy statement that we'll hopefully fill now.
  769. let mut stmt = Statement::default();
  770. let mut parsing = false;
  771. while let Some(token) = iter.next() {
  772. if !parsing {
  773. // TODO: MAKE SURE IT'S A SYMBOL
  774. // This logic must be changed if we want to support
  775. // multiple return values.
  776. if let Some(next_token) = iter.peek() {
  777. if next_token.token_type == TokenType::Assign {
  778. stmt.line = token.line;
  779. stmt.typ = StatementType::Assign;
  780. stmt.rhs = vec![];
  781. stmt.lhs = Some(Variable {
  782. name: token.token.clone(),
  783. typ: VarType::Dummy,
  784. line: token.line,
  785. column: token.column,
  786. });
  787. // Skip over the `=` token.
  788. iter.next();
  789. parsing = true;
  790. continue
  791. }
  792. if next_token.token_type == TokenType::LeftParen {
  793. stmt.line = token.line;
  794. stmt.typ = StatementType::Call;
  795. stmt.rhs = vec![];
  796. stmt.lhs = None;
  797. parsing = true;
  798. }
  799. if !parsing {
  800. return Err(self.error.abort(
  801. &format!("Illegal token `{}`.", next_token.token),
  802. next_token.line,
  803. next_token.column,
  804. ))
  805. }
  806. }
  807. }
  808. // If parsing == true, we now know if we're making a variable
  809. // assignment or a function call without a return value.
  810. // Let's dig deeper to see what the statement's call is, and
  811. // what it contains as arguments. With this we'll fill `rhs`.
  812. // The arguments could be literal types, other variables, an
  813. // array declaration, or even nested function calls.
  814. // For now, we don't care if the params are valid, as this is
  815. // the job of the semantic analyzer which comes after the
  816. // parsing module.
  817. // Array declaration.
  818. // TODO: Support function calls in array declarations. Currently
  819. // only literals can be used to construct an array.
  820. // Check only left_bracket. Validation to check that the brackets
  821. // are matched has already been performed above.
  822. if left_bracket > 0 {
  823. return Err(self.error.abort(
  824. "Arrays are not implemented yet.",
  825. token.line,
  826. token.column,
  827. ))
  828. //let rhs = self.parse_array_assignment(&mut iter);
  829. }
  830. // The assumption here is that the current token is a function
  831. // call, so we check if it's legit and start digging.
  832. let func_name = token.token.as_str();
  833. // Ensure the current function is a symbol
  834. if token.token_type != TokenType::Symbol {
  835. return Err(self.error.abort(
  836. "This token is not a symbol.",
  837. token.line,
  838. token.column,
  839. ))
  840. }
  841. if let Some(op) = Opcode::from_name(func_name) {
  842. let rhs = self.parse_function_call(token, &mut iter)?;
  843. stmt.opcode = op;
  844. stmt.rhs = rhs;
  845. } else {
  846. return Err(self.error.abort(
  847. &format!("Unimplemented opcode `{func_name}`."),
  848. token.line,
  849. token.column,
  850. ))
  851. }
  852. // At this stage of parsing, we should have assigned `stmt` a StatementType that is
  853. // not a Noop. If we have failed to do so, we cannot proceed because Nooops must
  854. // never be pased to the compiler. This can occur when multiple independent
  855. // statements are passed on one line, or if a statement is not terminated by a
  856. // semicolon.
  857. if stmt.typ == StatementType::Noop {
  858. return Err(self.error.abort(
  859. "Statement is a NOOP; not allowed. (Did you miss a semicolon?)",
  860. token.line,
  861. token.column,
  862. ))
  863. }
  864. ret.push(stmt);
  865. stmt = Statement::default();
  866. }
  867. }
  868. Ok(ret)
  869. }
  870. // fn parse_array_assignment(
  871. // &self,
  872. // iter: &mut Peekable<std::slice::Iter<'_, Token>>,
  873. // ) -> Result<Vec<Arg>> {
  874. // if let Some(next_token) = iter.peek() {
  875. // if next_token.token_type != TokenType::LeftBracket {
  876. // return Err(self.error.abort(
  877. // "Invalid array assignment opening. Must start with a '['.",
  878. // next_token.line,
  879. // next_token.column,
  880. // ))
  881. // }
  882. // // Skip the opening parenthesis
  883. // iter.next();
  884. // } else {
  885. // // TODO: Use token line number and column
  886. // return Err(self.error.abort("Premature ending of statement.", 0, 0))
  887. // }
  888. // todo!();
  889. // }
  890. fn parse_function_call(
  891. &self,
  892. token: &Token,
  893. iter: &mut Peekable<std::slice::Iter<'_, Token>>,
  894. ) -> Result<Vec<Arg>> {
  895. if let Some(next_token) = iter.peek() {
  896. if next_token.token_type != TokenType::LeftParen {
  897. return Err(self.error.abort(
  898. "Invalid function call opening. Must start with a '('.",
  899. next_token.line,
  900. next_token.column,
  901. ))
  902. }
  903. // Skip the opening parenthesis
  904. iter.next();
  905. } else {
  906. return Err(self.error.abort("Premature ending of statement.", token.line, token.column))
  907. }
  908. let mut ret = vec![];
  909. // The next element in the iter now hopefully contains an opcode
  910. // argument. If it's another opcode, we'll recurse into this
  911. // function's logic.
  912. // Otherwise, we look for variable and literal types.
  913. while let Some(arg) = iter.next() {
  914. // ============================
  915. // Parse a nested function call
  916. // ============================
  917. if let Some(op_inner) = Opcode::from_name(&arg.token) {
  918. if let Some(paren) = iter.peek() {
  919. if paren.token_type != TokenType::LeftParen {
  920. return Err(self.error.abort(
  921. "Invalid function call opening. Must start with a '('.",
  922. paren.line,
  923. paren.column,
  924. ))
  925. }
  926. // Recurse this function to get the params of the nested one.
  927. let args = self.parse_function_call(arg, iter)?;
  928. // Then we assign a "fake" variable that serves as a heap
  929. // reference.
  930. let var = Variable {
  931. name: format!("_op_inner_{}_{}", arg.line, arg.column),
  932. typ: VarType::Dummy,
  933. line: arg.line,
  934. column: arg.column,
  935. };
  936. let arg = Arg::Func(Statement {
  937. typ: StatementType::Assign,
  938. opcode: op_inner,
  939. lhs: Some(var),
  940. rhs: args,
  941. line: arg.line,
  942. });
  943. ret.push(arg);
  944. continue
  945. }
  946. return Err(self.error.abort(
  947. "Missing tokens in statement, there's a syntax error here.",
  948. arg.line,
  949. arg.column,
  950. ))
  951. }
  952. // ==========================================
  953. // Parse normal argument, not a function call
  954. // ==========================================
  955. if let Some(sep) = iter.next() {
  956. // See if we have a variable or a literal type.
  957. match arg.token_type {
  958. TokenType::Symbol => ret.push(Arg::Var(Variable {
  959. name: arg.token.clone(),
  960. typ: VarType::Dummy,
  961. line: arg.line,
  962. column: arg.column,
  963. })),
  964. TokenType::Number => {
  965. // Check if we can actually convert this into a number.
  966. match arg.token.parse::<u64>() {
  967. Ok(_) => {}
  968. Err(e) => {
  969. return Err(self.error.abort(
  970. &format!("Failed to convert literal into u64: {e}"),
  971. arg.line,
  972. arg.column,
  973. ))
  974. }
  975. };
  976. ret.push(Arg::Lit(Literal {
  977. name: arg.token.clone(),
  978. typ: LitType::Uint64,
  979. line: arg.line,
  980. column: arg.column,
  981. }))
  982. }
  983. TokenType::RightParen => {
  984. if let Some(comma) = iter.peek() {
  985. if comma.token_type == TokenType::Comma {
  986. iter.next();
  987. }
  988. }
  989. break
  990. }
  991. // Note: Unimplemented symbols throw an error now instead of a panic.
  992. // This assists with fuzz testing as existing features can still be tested
  993. // without causing the fuzzer to choke due to the panic created
  994. // by unimplmented!().
  995. // x => unimplemented!("{x:#?}"),
  996. _ => {
  997. return Err(self.error.abort(
  998. "Character is illegal/unimplemented in this context",
  999. arg.line,
  1000. arg.column,
  1001. ))
  1002. }
  1003. };
  1004. if sep.token_type == TokenType::RightParen {
  1005. if let Some(comma) = iter.peek() {
  1006. if comma.token_type == TokenType::Comma {
  1007. iter.next();
  1008. }
  1009. }
  1010. // Reached end of args
  1011. break
  1012. }
  1013. if sep.token_type != TokenType::Comma {
  1014. return Err(self.error.abort(
  1015. "Argument separator is not a comma (`,`)",
  1016. sep.line,
  1017. sep.column,
  1018. ))
  1019. }
  1020. }
  1021. }
  1022. Ok(ret)
  1023. }
  1024. }
  1025. trait NextTuple3<I>: Iterator<Item = I> {
  1026. fn next_tuple(&mut self) -> Option<(I, I, I)>;
  1027. }
  1028. impl<I: Iterator<Item = T>, T> NextTuple3<T> for I {
  1029. fn next_tuple(&mut self) -> Option<(T, T, T)> {
  1030. let a = self.next()?;
  1031. let b = self.next()?;
  1032. let c = self.next()?;
  1033. Some((a, b, c))
  1034. }
  1035. }
  1036. trait NextTuple4<I>: Iterator<Item = I> {
  1037. fn next_tuple(&mut self) -> Option<(I, I, I, I)>;
  1038. }
  1039. impl<I: Iterator<Item = T>, T> NextTuple4<T> for I {
  1040. fn next_tuple(&mut self) -> Option<(T, T, T, T)> {
  1041. let a = self.next()?;
  1042. let b = self.next()?;
  1043. let c = self.next()?;
  1044. let d = self.next()?;
  1045. Some((a, b, c, d))
  1046. }
  1047. }