Skip to content

Commit

Permalink
It's fucked but I still want to save my progress
Browse files Browse the repository at this point in the history
  • Loading branch information
3top1a committed Feb 8, 2025
1 parent ff0d091 commit 552af25
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 19 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- name: Test
run: just ci
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- name: Test
run: just ci
coverage:
name: coverage
runs-on: ubuntu-latest
Expand Down
6 changes: 4 additions & 2 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ Dup ; [5] [5]
Eq ; [1] Tests for equality
Match (Stack) ; Consumes top of stack
Case (0)
Print (65)
CaseDefault
Print (67)
Case (1)
Print (66)
Case (0)
Print (65)
EndMatch
Read (Stack) ; Pushes one byte of used input to stack
Expand Down
66 changes: 61 additions & 5 deletions src/lir/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ impl Codegen {
///
/// This consumes the Codegen instance and returns the generated code.
pub fn codegen(mut self, instructions: Instructions) -> String {
instructions.validate();

self.variables = instructions.get_variable_indexes().len();

// Allocate memory for variables
Expand All @@ -39,20 +41,24 @@ impl Codegen {
Push(n) => self.push(n),
Pop => self.pop(),
Dup => self.dup(),

Swap => self.swap(),
Binary {
op,
modified,
consumed,
} => self.binary(op, modified, consumed),

Copy { from, to } => self.copy(from, to),

Read(loc) => self.read(loc),

Print(val) => self.print(val),

_ => unimplemented!(),
Match(loc) => self.startmatch(loc),
EndCaseDefault => self.endcasedefault(),
CaseDefault => self.casedefault(),
Case(n) => self.case(n),
EndCase => self.endcase(),
EndMatch => self.endmatch(),

// _ => unimplemented!("Instruction not implemented: {:?}", instr),
}

self.code += "\n";
Expand Down Expand Up @@ -86,6 +92,11 @@ impl Codegen {
self.ptr += 1;
}

fn swap(&mut self) {
self.goto_stack();
self.code += "[->+<]<[->+<]>>[-<<+>>]<";
}

fn copy(&mut self, from: Value, to: Location) {
// TODO Refactor > Extract Method
match from {
Expand Down Expand Up @@ -205,4 +216,49 @@ impl Codegen {
}
}
}

fn startmatch(&mut self, loc: Location) {
/*
For example:
++++
>+<[
-[
[-]>-default#<
]>[-1#]<
]>[-0#]<
Every iteration we decrement and if it's zero we don't recurse further.
see https://brainfuck.org/function_tutorial.b
*/
match loc {
Location::Stack => {
self.goto_stack();
self.code += ">+<[";
self.ptr += 1;
}
_ => unimplemented!(),
}
}

fn endmatch(&mut self) {
// TODO
}

fn case(&mut self, n: u8) {
// TODO
}

fn endcase(&mut self) {
// TODO
}

fn casedefault(&mut self) {
// TODO
}

fn endcasedefault(&mut self) {
// TODO
}
}
14 changes: 7 additions & 7 deletions src/lir/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::lir::lir::{Instruction, Location, Value};

pub struct Instructions(pub Vec<Instruction>);
impl Instructions {
/// Validates instructions and returns if it's valid
/// Validates instructions and panics if invalid.
pub fn validate(&self) -> bool {
// Check for equal match and unmatch
let mut match_count = 0;
Expand All @@ -14,21 +14,21 @@ impl Instructions {
}
}
if match_count != 0 {
return false;
panic!("Unmatched match");
}

// Check that case (x) are ordered from 0 to n
let mut highest_case = 0;
// Check that case (x) are ordered from n to 0
let mut highest_case = 255;
for instr in &self.0 {
match instr {
Instruction::Case(x) => {
if *x > highest_case {
if *x < highest_case {
highest_case = *x;
} else {
return false;
panic!("Case not ordered");
}
}
Instruction::Match(_) => highest_case = 0,
Instruction::Match(_) => highest_case = 255,
_ => (),
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/lir/lir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum Instruction {
Push(Immediate),
Pop,
Dup,
Swap,

// Data manipulation
Binary {
Expand All @@ -39,6 +40,9 @@ pub enum Instruction {
// Control flow
Match(Location),
Case(Immediate),
EndCase,
CaseDefault,
EndCaseDefault,
EndMatch,
}

Expand All @@ -57,17 +61,21 @@ impl Instruction {
Instruction::Push(n) => format!("Push({})", n),
Instruction::Pop => "pop".to_string(),
Instruction::Dup => "Dup".to_string(),
Instruction::Swap => "Swap".to_string(),
Instruction::Binary {
op,
modified,
consumed,
} => format!("{:?}", op),
} => format!("{:?} {:?} {:?}", op, modified, consumed),
Instruction::Copy { from, to } => format!("Copy {:?} to {:?}", from, to),
Instruction::Read(loc) => format!("Read {:?}", loc),
Instruction::Print(val) => format!("Print {:?}", val),
Instruction::Match(loc) => format!("Match {:?}", loc),
Instruction::Case(n) => format!("Case {}", n),
Instruction::CaseDefault => "CaseDefault".to_string(),
Instruction::EndMatch => "EndMatch".to_string(),
Instruction::EndCase => "EndCase".to_string(),
Instruction::EndCaseDefault => "EndCaseDefault".to_string(),
}
}
}
43 changes: 43 additions & 0 deletions src/lir/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ mod tests {
test(vec![Read(Variable(0)), Read(Variable(1))], "><,>,");
}

#[test]
fn test_swap() {
test(
vec![Push(4), Push(9), Swap],
">++++>+++++++++[->+<]<[->+<]>>[-<<+>>]<",
);
// The stack should be [0] [9] [4]
}

#[test]
fn test_print() {
// Test print stack
Expand All @@ -122,4 +131,38 @@ mod tests {
">++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]<",
)
}

#[test]
fn test_match() {
// test(
// vec![
// Push(1),
// Match(Location::Stack),
// Case(0),
// Print(Value::Immediate(b'0')),
// Case(1),
// Print(Value::Immediate(b'1')),
// Case(2),
// Print(Value::Immediate(b'2')),
// CaseDefault,
// Print(Value::Immediate(b'D')),
// EndMatch,
// ],
// "",
// );



test(
vec![
Push(4),
Match(Location::Stack),
CaseDefault,
Case(1),
Case(0),
EndMatch,
],
">++++>+<[-[[-]>-default#<]>[-1#]<]>[-0#]<",
);
}
}

0 comments on commit 552af25

Please sign in to comment.