add hex constants and data segments

This commit is contained in:
2021-11-12 22:08:53 +01:00
parent 51cf8a8d28
commit e4bf292e47
14 changed files with 355 additions and 84 deletions

View File

@@ -7,6 +7,7 @@ pub struct Script {
pub imports: Vec<Import>,
pub global_vars: Vec<GlobalVar>,
pub functions: Vec<Function>,
pub data: Vec<Data>,
}
#[derive(Debug)]
@@ -14,6 +15,7 @@ pub enum TopLevelItem {
Import(Import),
GlobalVar(GlobalVar),
Function(Function),
Data(Data),
}
#[derive(Debug)]
@@ -57,6 +59,31 @@ pub struct Function {
pub body: Expression,
}
#[derive(Debug)]
pub struct Data {
pub offset: Box<Expression>,
pub data: Vec<DataValues>,
}
#[derive(Debug)]
pub enum DataValues {
Array {
type_: DataType,
values: Vec<Expression>,
},
String(String),
}
#[derive(Debug, Clone)]
pub enum DataType {
I8,
I16,
I32,
I64,
F32,
F64,
}
#[derive(Debug)]
pub struct MemoryLocation {
pub span: Span,
@@ -72,6 +99,36 @@ pub struct Expression {
pub span: Span,
}
impl Expression {
pub fn const_i32(&self) -> i32 {
match self.expr {
Expr::I32Const(v) => v,
_ => panic!("Expected I32Const")
}
}
pub fn const_i64(&self) -> i64 {
match self.expr {
Expr::I64Const(v) => v,
_ => panic!("Expected I64Const")
}
}
pub fn const_f32(&self) -> f32 {
match self.expr {
Expr::F32Const(v) => v,
_ => panic!("Expected F32Const")
}
}
pub fn const_f64(&self) -> f64 {
match self.expr {
Expr::F64Const(v) => v,
_ => panic!("Expected F64Const")
}
}
}
#[derive(Debug)]
pub enum Expr {
Block {
@@ -87,7 +144,7 @@ pub enum Expr {
name: String,
type_: Option<Type>,
value: Option<Box<Expression>>,
defer: bool,
let_type: LetType,
},
Poke {
mem_location: MemoryLocation,
@@ -143,7 +200,7 @@ pub enum Expr {
},
First {
value: Box<Expression>,
drop: Box<Expression>
drop: Box<Expression>,
},
Error,
}
@@ -158,6 +215,13 @@ impl Expr {
}
}
#[derive(Debug, Clone, Copy)]
pub enum LetType {
Normal,
Lazy,
Inline,
}
#[derive(Debug, Clone, Copy)]
pub enum UnaryOp {
Negate,