Просмотр исходного кода

zkas: Move some types to ast module.

parazyd 4 лет назад
Родитель
Сommit
7546e641c5
3 измененных файлов с 37 добавлено и 57 удалено
  1. 26 1
      zkas/src/ast.rs
  2. 11 40
      zkas/src/parser.rs
  3. 0 16
      zkas/src/types.rs

+ 26 - 1
zkas/src/ast.rs

@@ -1,4 +1,6 @@
-use crate::opcode::Opcode;
+use std::collections::HashMap;
+
+use crate::{lexer::Token, opcode::Opcode, types::Type};
 
 #[derive(PartialEq, Clone, Debug)]
 pub enum StatementType {
@@ -34,3 +36,26 @@ impl Default for Statement {
         }
     }
 }
+
+pub type UnparsedConstants = HashMap<String, (Token, Token)>;
+pub type UnparsedWitnesses = HashMap<String, (Token, Token)>;
+
+pub type Constants = Vec<Constant>;
+pub type Witnesses = Vec<Witness>;
+pub type Statements = Vec<Statement>;
+
+#[derive(Clone, Debug)]
+pub struct Constant {
+    pub name: String,
+    pub typ: Type,
+    pub line: usize,
+    pub column: usize,
+}
+
+#[derive(Clone, Debug)]
+pub struct Witness {
+    pub name: String,
+    pub typ: Type,
+    pub line: usize,
+    pub column: usize,
+}

+ 11 - 40
zkas/src/parser.rs

@@ -4,20 +4,15 @@ use itertools::Itertools;
 use termion::{color, style};
 
 use crate::{
-    ast::{Statement, StatementType, Variable},
+    ast::{
+        Constant, Constants, Statement, StatementType, Statements, UnparsedConstants,
+        UnparsedWitnesses, Variable, Witness, Witnesses,
+    },
     lexer::{Token, TokenType},
     opcode::Opcode,
-    types::{Constant, Type, Witness},
+    types::Type,
 };
 
-pub type Ast = HashMap<String, HashMap<String, HashMap<String, (Token, Token)>>>;
-
-pub type UnparsedConstants = HashMap<String, (Token, Token)>;
-pub type Constants = Vec<Constant>;
-
-pub type UnparsedWitnesses = HashMap<String, (Token, Token)>;
-pub type Witnesses = Vec<Witness>;
-
 pub struct Parser {
     file: String,
     lines: Vec<String>,
@@ -32,7 +27,7 @@ impl Parser {
         Parser { file: filename.to_string(), lines, tokens }
     }
 
-    pub fn parse(self) -> (Constants, Witnesses, Ast) {
+    pub fn parse(self) -> (Constants, Witnesses, Statements) {
         // We use these to keep state when iterating
         let mut declaring_constant = false;
         let mut declaring_contract = false;
@@ -254,7 +249,8 @@ impl Parser {
         }
 
         ast.insert(namespace.clone(), ast_inner);
-        self.verify_initial_ast(&ast);
+        // TODO: Verify there are both constant/contract sections
+        // TODO: Verify there is a circuit section
 
         // Clean up the `constant` section
         let c = ast.get(&namespace).unwrap().get("constant").unwrap();
@@ -265,29 +261,9 @@ impl Parser {
         let witnesses = self.parse_ast_contract(c);
 
         // Clean up the `circuit` section
-        let circuit =
-            self.parse_ast_circuit(constants.clone(), witnesses.clone(), circuit_statements);
+        let stmt = self.parse_ast_circuit(circuit_statements);
 
-        (constants, witnesses, HashMap::new())
-    }
-
-    fn verify_initial_ast(&self, ast: &Ast) {
-        // Verify that there are all 3 sections
-        for v in ast.values() {
-            if !v.contains_key("constant") {
-                self.error("Missing `constant` section in the source.".to_string(), 1, 0);
-            }
-
-            if !v.contains_key("contract") {
-                self.error("Missing `contract` section in the source.".to_string(), 1, 0);
-            }
-
-            /*
-            if !v.contains_key("circuit") {
-                self.error("Missing `circuit` section in the source.".to_string(), 1, 1);
-            }
-            */
-        }
+        (constants, witnesses, stmt)
     }
 
     fn check_section_structure(&self, section: &str, tokens: Vec<Token>) {
@@ -451,12 +427,7 @@ impl Parser {
         ret
     }
 
-    fn parse_ast_circuit(
-        &self,
-        constants: Constants,
-        witnesses: Witnesses,
-        statements: Vec<Vec<Token>>,
-    ) -> Vec<Statement> {
+    fn parse_ast_circuit(&self, statements: Vec<Vec<Token>>) -> Vec<Statement> {
         // 1. Scan the tokens to map opcodes (function calls)
         // 2. For each statement, see if there are variable assignments
         // 3. When referencing, check if they're in Constants, Witnesses

+ 0 - 16
zkas/src/types.rs

@@ -6,19 +6,3 @@ pub enum Type {
     Scalar = 0x02,
     MerklePath = 0x03,
 }
-
-#[derive(Clone, Debug)]
-pub struct Constant {
-    pub name: String,
-    pub typ: Type,
-    pub line: usize,
-    pub column: usize,
-}
-
-#[derive(Clone, Debug)]
-pub struct Witness {
-    pub name: String,
-    pub typ: Type,
-    pub line: usize,
-    pub column: usize,
-}