Skip to content

Commit

Permalink
add support for out-of-order constant declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Jul 19, 2022
1 parent d1b8c64 commit b6ac563
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "customasm"
version = "0.11.15"
version = "0.12.0"
edition = "2021"
authors = ["hlorenzi <https://hlorenzi.com>"]
description = "An assembler for custom, user-defined instruction sets!"
Expand Down
9 changes: 9 additions & 0 deletions src/asm/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum InvocationKind
Rule(RuleInvocation),
Data(DataInvocation),
Label(LabelInvocation),
Constant(ConstantInvocation),
}


Expand Down Expand Up @@ -57,6 +58,14 @@ pub struct DataInvocation
pub struct LabelInvocation;


#[derive(Debug)]
pub struct ConstantInvocation
{
pub expr: expr::Expr,
pub value_guess: expr::Value,
}


impl Invocation
{
pub fn get_rule_invoc(&self) -> &RuleInvocation
Expand Down
1 change: 1 addition & 0 deletions src/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub use self::invocation::RuleInvocationCandidate;
pub use self::invocation::RuleInvocationArgument;
pub use self::invocation::DataInvocation;
pub use self::invocation::LabelInvocation;
pub use self::invocation::ConstantInvocation;
pub use self::bank::Bank;
pub use self::bank::BankData;
pub use self::symbol::SymbolManager;
Expand Down
14 changes: 13 additions & 1 deletion src/asm/parser/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,19 @@ pub fn parse_symbol(
&ctx,
&mut expr::EvalContext::new(),
state.fileserver,
true)?;
false)?;

let bankdata = state.asm_state.get_bankdata_mut(state.asm_state.cur_bank);
bankdata.push_invocation(asm::Invocation
{
ctx: ctx.clone(),
size_guess: 0,
span: span.clone(),
kind: asm::InvocationKind::Constant(asm::ConstantInvocation {
expr,
value_guess: value.clone(),
}),
});

state.parser.expect_linebreak()?;
value
Expand Down
20 changes: 20 additions & 0 deletions src/asm/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,26 @@ impl State

continue;
}

asm::InvocationKind::Constant(ref constant_invoc) =>
{
let final_value = self.eval_expr(
report.clone(),
&constant_invoc.expr,
&invoc.ctx,
&mut expr::EvalContext::new(),
fileserver,
true)?;

if final_value != constant_invoc.value_guess
{
report.error_span(
"constant value did not converge after iterations",
&invoc.span);
}

continue;
}
};

let resolved = match maybe_resolved
Expand Down
3 changes: 3 additions & 0 deletions tests/symbol_constant_outoforder/err1_unknown_y.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = y + y ; error: unknown

#d8 x
5 changes: 5 additions & 0 deletions tests/symbol_constant_outoforder/err2_unknown_z.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x = y + y
y = z + z ; error: unknown

#d8 x
#d8 y
6 changes: 6 additions & 0 deletions tests/symbol_constant_outoforder/err3_cyclical.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x = y + 1 ; error: converge
y = z + 1 ; error: converge
z = x + 1

#d8 x
#d8 y
20 changes: 20 additions & 0 deletions tests/symbol_constant_outoforder/err4_cascading_instr.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ruledef test
{
ld {x} =>
{
assert(x <= 0x8)
0x11 @ x`16
}

ld {x} =>
{
assert(x > 0x8)
0x22 @ x`8
}
}

x = y ; error: converge
ld x
ld x
label:
y = label + 3
20 changes: 20 additions & 0 deletions tests/symbol_constant_outoforder/err5_cascading_instr.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ruledef test
{
ld {x} =>
{
assert(x <= 0x8)
0x11 @ x`16
}

ld {x} =>
{
assert(x > 0x8)
0x22 @ x`8
}
}

ld x ; error: converge
ld x ; error: converge
x = y
label:
y = label + 3
5 changes: 5 additions & 0 deletions tests/symbol_constant_outoforder/ok1_xy.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x = y + y
y = 1

#d8 x ; = 0x02
#d8 y ; = 0x01
7 changes: 7 additions & 0 deletions tests/symbol_constant_outoforder/ok2_xyz.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x = y + y
y = z + z
z = 1

#d8 x ; = 0x04
#d8 y ; = 0x02
#d8 z ; = 0x01
5 changes: 5 additions & 0 deletions tests/symbol_constant_outoforder/ok3_cyclical_allzero.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x = y
y = x

#d8 x ; = 0x00
#d8 y ; = 0x00
24 changes: 24 additions & 0 deletions tests/symbol_constant_outoforder/ok4_cascading_instr.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ruledef test
{
ld {x} =>
{
assert(x <= 0x8)
0x11 @ x`16
}

ld {x} =>
{
assert(x > 0x8)
0x22 @ x`8
}
}

x1 = y1 + y1
ld x1 ; = 0x2210
ld x1 ; = 0x2210
y1 = 8

ld x2 ; = 0x110002
ld x2 ; = 0x110002
x2 = y2 + y2
y2 = 1
20 changes: 20 additions & 0 deletions tests/symbol_constant_outoforder/ok5_cascading_instr.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ruledef test
{
ld {x} =>
{
assert(x <= 0x8)
0x11 @ x`16
}

ld {x} =>
{
assert(x > 0x8)
0x22 @ x`8
}
}

x = y
ld x ; = 0x110006
ld x ; = 0x110006
y = label
label:
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 0 additions & 10 deletions tests/symbol_variable_simple/4.asm

This file was deleted.

0 comments on commit b6ac563

Please sign in to comment.