From b8afed80194cb7270bf5ec9cac18c2254cf7c5dd Mon Sep 17 00:00:00 2001 From: Per Lindgren <per.lindgren@ltu.se> Date: Sun, 13 Sep 2020 13:24:09 +0200 Subject: [PATCH] fallible wip1 --- src/ast/ast.rs | 14 ++++++++++++-- src/ast/main.rs | 27 +++++++++++++++++++++++++++ src/ast/parser.lalrpop | 4 ++-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/ast/ast.rs b/src/ast/ast.rs index 6cdec50..b18fb60 100644 --- a/src/ast/ast.rs +++ b/src/ast/ast.rs @@ -2,11 +2,13 @@ use std::fmt; // ast +pub type Id = String; + // println!("{:?}", ..) #[derive(Debug)] pub enum NumOrId { - Num(usize), - Id(String), + Num(i32), + Id(Id), } // println!("{}", ..) @@ -19,3 +21,11 @@ impl fmt::Display for NumOrId { Ok(()) } } + +pub type Stmts = Vec<Stmt>; + +#[derive(Debug)] +pub enum Stmt { + Let(Id, NumOrId), + If(Id, Stmts, Option<Stmts>), +} diff --git a/src/ast/main.rs b/src/ast/main.rs index 68f3896..4b310e9 100644 --- a/src/ast/main.rs +++ b/src/ast/main.rs @@ -5,6 +5,7 @@ lalrpop_mod!(pub parser, "/ast/parser.rs"); use parser::*; pub mod ast; +use ast::*; fn main() { println!("minimal"); @@ -26,3 +27,29 @@ fn parse_num_or_id() { "a1_a" ); } + +type Error = String; + +fn type_check(stmts: &Stmts) -> Result<(), Error> { + for i in stmts { + match i { + Stmt::Let(_, _) => { + println!("let"); + } + Stmt::If(_, _, __) => { + println!("if"); + } + } + } + Ok(()) +} + +#[test] +fn test_stmts() { + let stmts = vec![ + Stmt::Let("a".to_string(), NumOrId::Num(1)), + Stmt::Let("b".to_string(), NumOrId::Num(2)), + Stmt::Let("c".to_string(), NumOrId::Num(3)), + ]; + type_check(&stmts).unwrap(); +} diff --git a/src/ast/parser.lalrpop b/src/ast/parser.lalrpop index 87f0b32..3405c21 100644 --- a/src/ast/parser.lalrpop +++ b/src/ast/parser.lalrpop @@ -9,8 +9,8 @@ pub NumOrId: NumOrId = { Id => NumOrId::Id(<>), } -pub Num: usize = { - r"[0-9]+" => usize::from_str(<>).unwrap(), +pub Num: i32 = { + r"[0-9]+" => i32::from_str(<>).unwrap(), }; pub Id: String = { -- GitLab