Skip to content

Commit

Permalink
feat: all types
Browse files Browse the repository at this point in the history
  • Loading branch information
SpideyZac committed Jun 4, 2024
1 parent 22917f4 commit 1e5a016
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/compiler/target/vm/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ void machine_mov(machine *vm) {
float value = machine_pop(vm);

vm->stack[vm->base_ptr - offset] = value;
// print stack
// for (int i = 0; i < vm->stack_pointer; i++) {
// printf("%f\n", vm->stack[i]);
// }
// printf("\n");
}

void machine_add(machine *vm) {
Expand Down
97 changes: 96 additions & 1 deletion src/compiler/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub struct Visitor<'a> {
ir: ir::IR,
errors: Vec<VisitorError>,
program_state: ProgramState,
first_it_yarn: bool,
}

impl<'a> Visitor<'a> {
Expand Down Expand Up @@ -118,6 +119,7 @@ impl<'a> Visitor<'a> {
},
function_states: HashMap::new(),
},
first_it_yarn: true,
};

visitor
Expand Down Expand Up @@ -192,7 +194,8 @@ impl<'a> Visitor<'a> {
pub fn visit_statement(&mut self, statement: ast::StatementNode) {
match statement.value {
ast::StatementNodeValueOption::Expression(expr) => {
let (type_, _) = self.visit_expression(expr.clone());
let (type_, _) = self
.visit_expression(expr.clone(), if self.first_it_yarn { false } else { true });
// save to IT with type_
if type_ != VariableTypes::Noob {
let scope = self.program_state.get_scope();
Expand All @@ -206,6 +209,31 @@ impl<'a> Visitor<'a> {
ir::IRStatement::Mov,
]);
}
VariableTypes::Numbar => {
self.add_statements(vec![
ir::IRStatement::Push(
*scope.variable_addresses.get("IT_NUMBAR").unwrap() as f32,
),
ir::IRStatement::Mov,
]);
}
VariableTypes::Yarn => {
self.first_it_yarn = false;
self.add_statements(vec![
ir::IRStatement::Push(
*scope.variable_addresses.get("IT_YARN").unwrap() as f32,
),
ir::IRStatement::Mov,
]);
}
VariableTypes::Troof => {
self.add_statements(vec![
ir::IRStatement::Push(
*scope.variable_addresses.get("IT_TROOF").unwrap() as f32,
),
ir::IRStatement::Mov,
]);
}
_ => {
panic!("Unexpected type")
}
Expand All @@ -224,12 +252,44 @@ impl<'a> Visitor<'a> {
pub fn visit_expression(
&mut self,
expression: ast::ExpressionNode,
string_free: bool,
) -> (VariableTypes, ast::TokenNode) {
match expression.value {
ast::ExpressionNodeValueOption::NumberValue(number_value) => {
self.visit_number_value(number_value.clone());
(VariableTypes::Number, number_value.token.clone())
}
ast::ExpressionNodeValueOption::NumbarValue(numbar_value) => {
self.visit_numbar_value(numbar_value.clone());
(VariableTypes::Numbar, numbar_value.token.clone())
}
ast::ExpressionNodeValueOption::YarnValue(yarn_value) => {
if string_free {
let scope = self.program_state.get_scope();
self.add_statements(vec![
ir::IRStatement::Push(
*scope.variable_addresses.get("IT_YARN").unwrap() as f32
),
ir::IRStatement::Copy,
ir::IRStatement::Load(1), // get the size
ir::IRStatement::Push(1.0),
ir::IRStatement::Add,
ir::IRStatement::Push(4.0),
ir::IRStatement::Multiply,
ir::IRStatement::Push(
*scope.variable_addresses.get("IT_YARN").unwrap() as f32
),
ir::IRStatement::Copy,
ir::IRStatement::Free,
]);
}
self.visit_yarn_value(yarn_value.clone());
(VariableTypes::Yarn, yarn_value.token.clone())
}
ast::ExpressionNodeValueOption::TroofValue(troof_value) => {
self.visit_troof_value(troof_value.clone());
(VariableTypes::Troof, troof_value.token.clone())
}
_ => {
panic!("Unexpected expression")
}
Expand All @@ -239,4 +299,39 @@ impl<'a> Visitor<'a> {
pub fn visit_number_value(&mut self, number_value: ast::NumberValueNode) {
self.add_statements(vec![ir::IRStatement::Push(number_value.value() as f32)]);
}

pub fn visit_numbar_value(&mut self, numbar_value: ast::NumbarValueNode) {
self.add_statements(vec![ir::IRStatement::Push(numbar_value.value())]);
}

pub fn visit_yarn_value(&mut self, yarn_value: ast::YarnValueNode) {
// yarn stores a pointer to the string on the heap
let chars = yarn_value.value().chars().collect::<Vec<char>>();
self.add_statements(vec![
ir::IRStatement::Push((chars.len() as i32 as f32 + 1.0) * 4.0), // store length + 1
ir::IRStatement::Allocate, // allocate space on the heap
]);
self.add_statements(vec![ir::IRStatement::Push(chars.len() as i32 as f32)]); // store length
for char in chars.iter() {
self.add_statements(vec![ir::IRStatement::Push(*char as i32 as f32)]);
// store char
}
self.add_statements(vec![
ir::IRStatement::Push(
-(self.program_state.get_scope().variables as f32
- self.program_state.get_scope().arguments as f32
+ 1.0),
), // This is the address of the heap_ptr for the string
ir::IRStatement::Copy, // duplicate this value
ir::IRStatement::Store(chars.len() as i32 + 1), // store the string at the address
]);
}

pub fn visit_troof_value(&mut self, troof_value: ast::TroofValueNode) {
self.add_statements(vec![ir::IRStatement::Push(if troof_value.value() {
1.0
} else {
0.0
})]);
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::parser::parser as p;
use crate::utils::get_line;

fn main() {
let contents = "HAI 1.2\n1\nKTHXBYE";
let contents = "HAI 1.2\n1, \"hello WORLD!\", \"JA!\", \"HEY!\", WIN, FAIL\nKTHXBYE";
let lines = contents.split("\n").collect::<Vec<&str>>();

let mut l = l::Lexer::init(contents);
Expand Down

0 comments on commit 1e5a016

Please sign in to comment.