From af1c8f999bb2401178ad4160119fec058a2012ea Mon Sep 17 00:00:00 2001 From: Dennis Ranke Date: Tue, 26 Oct 2021 20:29:57 +0200 Subject: [PATCH] first running uw8 module --- src/ast.rs | 10 +++++++--- src/emit.rs | 10 +++++++--- src/parser.rs | 11 +++++++++-- src/typecheck.rs | 4 ++-- test.hw | 6 +++--- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 5f6f84b..a9a27a9 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -5,7 +5,7 @@ pub struct Position(pub usize); pub struct Script<'a> { pub imports: Vec>, pub global_vars: Vec>, - pub functions: Vec> + pub functions: Vec>, } #[derive(Debug)] @@ -19,13 +19,17 @@ pub enum TopLevelItem<'a> { pub struct Import<'a> { pub position: Position, pub import: &'a str, - pub type_: ImportType<'a> + pub type_: ImportType<'a>, } #[derive(Debug)] pub enum ImportType<'a> { Memory(u32), - Variable {name: &'a str, type_: Type}, + Variable { + name: &'a str, + type_: Type, + mutable: bool, + }, // Function { name: &'a str, params: Vec, result: Option } } diff --git a/src/emit.rs b/src/emit.rs index fe2be25..dbb3ba4 100644 --- a/src/emit.rs +++ b/src/emit.rs @@ -44,11 +44,15 @@ pub fn emit(script: &ast::Script) -> Vec { memory64: false, } .into(), - ast::ImportType::Variable { type_, name } => { + ast::ImportType::Variable { + type_, + name, + mutable, + } => { globals.insert(name, globals.len() as u32); GlobalType { val_type: map_type(type_), - mutable: false, + mutable, } .into() } @@ -197,8 +201,8 @@ fn emit_block(ctx: &mut FunctionContext, block: &ast::Block) { value, .. } => { - emit_expression(ctx, value); emit_expression(ctx, &mem_location.left); + emit_expression(ctx, value); let offset = if let ast::Expr::I32Const(v) = mem_location.right.expr { v as u32 as u64 } else { diff --git a/src/parser.rs b/src/parser.rs index 15401fd..a7e14ef 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -66,9 +66,16 @@ fn import(s: &str) -> IResult { map( preceded( ws(tag("global")), - pair(identifier, preceded(ws(char(':')), type_)), + pair( + pair(opt(ws(tag("mut"))), identifier), + preceded(ws(char(':')), type_), + ), ), - |(name, type_)| ast::ImportType::Variable { name, type_ }, + |((mutable, name), type_)| ast::ImportType::Variable { + name, + type_, + mutable: mutable.is_some(), + }, ), ))(s)?; let (s, _) = ws(char(';'))(s)?; diff --git a/src/typecheck.rs b/src/typecheck.rs index b9d979e..8f25431 100644 --- a/src/typecheck.rs +++ b/src/typecheck.rs @@ -21,7 +21,7 @@ pub fn tc_script(script: &mut ast::Script) -> Result<()> { for import in &script.imports { match import.type_ { - ast::ImportType::Variable { name, type_ } => { + ast::ImportType::Variable { name, type_, .. } => { if context.global_vars.contains_key(name) { return Err(Error { position: import.position, @@ -31,7 +31,7 @@ pub fn tc_script(script: &mut ast::Script) -> Result<()> { context.global_vars.insert(name, type_); } // ast::ImportType::Function { .. } => todo!(), - ast::ImportType::Memory( .. ) => () + ast::ImportType::Memory(..) => (), } } diff --git a/test.hw b/test.hw index e8ebf03..d4175fb 100644 --- a/test.hw +++ b/test.hw @@ -1,10 +1,10 @@ import "uw8.ram" memory(2); -import "uw8.time" global time: i32; +import "uw8.time" global mut time: i32; export fn tic() { - let i = 0; + let i: i32; loop frame { - i?64 = (i % 320 + time / 10) ^ (i / 320); + i?120 = (i % 320 + time / 10) ^ (i / 320); branch_if (i := i + 1) < 320*256: frame; } }