diff --git a/src/ast/ast.rs b/src/ast/ast.rs index 6cdec503367e5f0f3ad9249e208e1734f1a14137..b18fb6070692a9a4fe2cec834a4a4995aaa12147 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 68f38969dca28eb986b37d9dab2d9a3c51543af4..4b310e9a9bd5fc27863f6fdec9b9e4c3d8fbcca2 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 87f0b32f26126cf26e612b5fc40cdda48d17a881..3405c21c734b91e90376569d4f87f49eee6083b6 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 = {