Bladeren bron

zkas: Outline parser behaviour and add opcodes+types.

parazyd 4 jaren geleden
bovenliggende
commit
c3188b7576
4 gewijzigde bestanden met toevoegingen van 69 en 2 verwijderingen
  1. 2 0
      zkas/src/lib.rs
  2. 2 0
      zkas/src/opcode.rs
  3. 59 2
      zkas/src/parser.rs
  4. 6 0
      zkas/src/types.rs

+ 2 - 0
zkas/src/lib.rs

@@ -1,3 +1,5 @@
 pub mod error;
 pub mod lexer;
+pub mod opcode;
 pub mod parser;
+pub mod types;

+ 2 - 0
zkas/src/opcode.rs

@@ -7,5 +7,7 @@ pub enum OpCode {
 
     PoseidonHash = 0x10,
 
+    CalculateMerkleRoot = 0x20,
+
     ConstrainInstance = 0xf0,
 }

+ 59 - 2
zkas/src/parser.rs

@@ -9,20 +9,77 @@ pub fn parse(filename: &str, source: Chars, tokens: Vec<Token>) {
     // For nice error reporting, we'll load everything into a string vector
     // so we have references to lines.
     let lines: Vec<String> = source.as_str().lines().map(|x| x.to_string()).collect();
-    let parser_error = ParserError::new(filename, lines);
+    let _parser_error = ParserError::new(filename, lines);
 
     // We use these to keep state when iterating
     let mut declaring_constant = false;
     let mut declaring_contract = false;
     let mut declaring_circuit = false;
 
+    let mut constant_tokens = vec![];
+    let mut contract_tokens = vec![];
+    let mut circuit_tokens = vec![];
+
     let mut iter = tokens.iter();
     while let Some(t) = iter.next() {
         // Start by declaring a section
         if !declaring_constant && !declaring_contract && !declaring_circuit {
             if t.token_type != TokenType::Symbol {
-                println!("FOO");
+                // TODO: Revisit
+                // TODO: Visit this again when we are allowing imports
+                panic!();
+            }
+
+            match t.token.as_str() {
+                "constant" => {
+                    declaring_constant = true;
+                    //while let Some(inner) = iter.next() {
+                    for inner in iter.by_ref() {
+                        constant_tokens.push(inner);
+                        if inner.token_type == TokenType::RightBrace {
+                            break
+                        }
+                    }
+                }
+
+                "contract" => {
+                    declaring_contract = true;
+                    //while let Some(inner) = iter.next() {
+                    for inner in iter.by_ref() {
+                        contract_tokens.push(inner);
+                        if inner.token_type == TokenType::RightBrace {
+                            break
+                        }
+                    }
+                }
+
+                "circuit" => {
+                    declaring_circuit = true;
+                    //while let Some(inner) = iter.next() {
+                    for inner in iter.by_ref() {
+                        circuit_tokens.push(inner);
+                        if inner.token_type == TokenType::RightBrace {
+                            break
+                        }
+                    }
+                }
+
+                // Fall through
+                _ => unreachable!(),
             }
         }
+
+        // We shouldn't be reaching these states
+        if declaring_constant && (declaring_contract || declaring_circuit) {
+            unreachable!()
+        }
+        if declaring_contract && (declaring_constant || declaring_circuit) {
+            unreachable!()
+        }
+        if declaring_circuit && (declaring_constant || declaring_contract) {
+            unreachable!()
+        }
+
+        // Now go through the token vectors and work it through
     }
 }

+ 6 - 0
zkas/src/types.rs

@@ -0,0 +1,6 @@
+pub enum Type {
+    EcFixedPoint = 0x00,
+    Base = 0x01,
+    Scalar = 0x02,
+    MerklePath = 0x03,
+}