mirror of
https://github.com/exoticorn/curlywas.git
synced 2026-01-20 11:46:43 +01:00
first running uw8 module
This commit is contained in:
10
src/ast.rs
10
src/ast.rs
@@ -5,7 +5,7 @@ pub struct Position(pub usize);
|
|||||||
pub struct Script<'a> {
|
pub struct Script<'a> {
|
||||||
pub imports: Vec<Import<'a>>,
|
pub imports: Vec<Import<'a>>,
|
||||||
pub global_vars: Vec<GlobalVar<'a>>,
|
pub global_vars: Vec<GlobalVar<'a>>,
|
||||||
pub functions: Vec<Function<'a>>
|
pub functions: Vec<Function<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -19,13 +19,17 @@ pub enum TopLevelItem<'a> {
|
|||||||
pub struct Import<'a> {
|
pub struct Import<'a> {
|
||||||
pub position: Position,
|
pub position: Position,
|
||||||
pub import: &'a str,
|
pub import: &'a str,
|
||||||
pub type_: ImportType<'a>
|
pub type_: ImportType<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ImportType<'a> {
|
pub enum ImportType<'a> {
|
||||||
Memory(u32),
|
Memory(u32),
|
||||||
Variable {name: &'a str, type_: Type},
|
Variable {
|
||||||
|
name: &'a str,
|
||||||
|
type_: Type,
|
||||||
|
mutable: bool,
|
||||||
|
},
|
||||||
// Function { name: &'a str, params: Vec<Type>, result: Option<Type> }
|
// Function { name: &'a str, params: Vec<Type>, result: Option<Type> }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
src/emit.rs
10
src/emit.rs
@@ -44,11 +44,15 @@ pub fn emit(script: &ast::Script) -> Vec<u8> {
|
|||||||
memory64: false,
|
memory64: false,
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
ast::ImportType::Variable { type_, name } => {
|
ast::ImportType::Variable {
|
||||||
|
type_,
|
||||||
|
name,
|
||||||
|
mutable,
|
||||||
|
} => {
|
||||||
globals.insert(name, globals.len() as u32);
|
globals.insert(name, globals.len() as u32);
|
||||||
GlobalType {
|
GlobalType {
|
||||||
val_type: map_type(type_),
|
val_type: map_type(type_),
|
||||||
mutable: false,
|
mutable,
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
@@ -197,8 +201,8 @@ fn emit_block(ctx: &mut FunctionContext, block: &ast::Block) {
|
|||||||
value,
|
value,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
emit_expression(ctx, value);
|
|
||||||
emit_expression(ctx, &mem_location.left);
|
emit_expression(ctx, &mem_location.left);
|
||||||
|
emit_expression(ctx, value);
|
||||||
let offset = if let ast::Expr::I32Const(v) = mem_location.right.expr {
|
let offset = if let ast::Expr::I32Const(v) = mem_location.right.expr {
|
||||||
v as u32 as u64
|
v as u32 as u64
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -66,9 +66,16 @@ fn import(s: &str) -> IResult<ast::Import> {
|
|||||||
map(
|
map(
|
||||||
preceded(
|
preceded(
|
||||||
ws(tag("global")),
|
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)?;
|
))(s)?;
|
||||||
let (s, _) = ws(char(';'))(s)?;
|
let (s, _) = ws(char(';'))(s)?;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ pub fn tc_script(script: &mut ast::Script) -> Result<()> {
|
|||||||
|
|
||||||
for import in &script.imports {
|
for import in &script.imports {
|
||||||
match import.type_ {
|
match import.type_ {
|
||||||
ast::ImportType::Variable { name, type_ } => {
|
ast::ImportType::Variable { name, type_, .. } => {
|
||||||
if context.global_vars.contains_key(name) {
|
if context.global_vars.contains_key(name) {
|
||||||
return Err(Error {
|
return Err(Error {
|
||||||
position: import.position,
|
position: import.position,
|
||||||
@@ -31,7 +31,7 @@ pub fn tc_script(script: &mut ast::Script) -> Result<()> {
|
|||||||
context.global_vars.insert(name, type_);
|
context.global_vars.insert(name, type_);
|
||||||
}
|
}
|
||||||
// ast::ImportType::Function { .. } => todo!(),
|
// ast::ImportType::Function { .. } => todo!(),
|
||||||
ast::ImportType::Memory( .. ) => ()
|
ast::ImportType::Memory(..) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
6
test.hw
6
test.hw
@@ -1,10 +1,10 @@
|
|||||||
import "uw8.ram" memory(2);
|
import "uw8.ram" memory(2);
|
||||||
import "uw8.time" global time: i32;
|
import "uw8.time" global mut time: i32;
|
||||||
|
|
||||||
export fn tic() {
|
export fn tic() {
|
||||||
let i = 0;
|
let i: i32;
|
||||||
loop frame {
|
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;
|
branch_if (i := i + 1) < 320*256: frame;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user