Răsfoiți Sursa

zkas/analyzer: Add error functions.

parazyd 4 ani în urmă
părinte
comite
bbecf641de
2 a modificat fișierele cu 36 adăugiri și 6 ștergeri
  1. 31 2
      zkas/src/analyzer.rs
  2. 5 4
      zkas/src/types.rs

+ 31 - 2
zkas/src/analyzer.rs

@@ -1,4 +1,6 @@
-use std::str::Chars;
+use std::{io, io::Write, process, str::Chars};
+
+use termion::{color, style};
 
 use crate::ast::{Constants, Statements, Witnesses};
 
@@ -24,5 +26,32 @@ impl Analyzer {
         Analyzer { file: filename.to_string(), lines, constants, witnesses, statements }
     }
 
-    pub fn analyze(self) {}
+    pub fn analyze(self) {
+        self.error("Semantic analyzer not implemented".to_string(), 1, 0);
+    }
+
+    fn error(&self, msg: String, ln: usize, col: usize) {
+        let err_msg = format!("{} (line {}, column {})", msg, ln, col);
+        let dbg_msg = format!("{}:{}:{}: {}", self.file, ln, col, self.lines[ln - 1]);
+        let pad = dbg_msg.split(": ").next().unwrap().len() + col + 2;
+        let caret = format!("{:width$}^", "", width = pad);
+        let msg = format!("{}\n{}\n{}\n", err_msg, dbg_msg, caret);
+        Analyzer::abort(&msg);
+    }
+
+    fn abort(msg: &str) {
+        let stderr = io::stderr();
+        let mut handle = stderr.lock();
+        write!(
+            handle,
+            "{}{}Semantic error:{} {}",
+            style::Bold,
+            color::Fg(color::Red),
+            style::Reset,
+            msg,
+        )
+        .unwrap();
+        handle.flush().unwrap();
+        process::exit(1);
+    }
 }

+ 5 - 4
zkas/src/types.rs

@@ -1,8 +1,9 @@
 /// Types supported by the VM
 #[derive(Clone, Debug)]
 pub enum Type {
-    EcFixedPoint = 0x00,
-    Base = 0x01,
-    Scalar = 0x02,
-    MerklePath = 0x03,
+    EcPoint = 0x00,
+    EcFixedPoint = 0x01,
+    Base = 0x10,
+    Scalar = 0x11,
+    MerklePath = 0x20,
 }