Hopefully made math recursively work and added FasmCodegen
struct.
This commit is contained in:
parent
1a8d7498e5
commit
dd1bc0b478
|
@ -2,6 +2,7 @@
|
||||||
name = "skylang"
|
name = "skylang"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
channel = "nightly"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
|
@ -1,66 +0,0 @@
|
||||||
#![allow(warnings)]
|
|
||||||
|
|
||||||
pub mod lex;
|
|
||||||
pub mod codegen;
|
|
||||||
use crate::codegen::fasm::*;
|
|
||||||
use crate::lex::tok::*;
|
|
||||||
use crate::parse::ast::*;
|
|
||||||
use crate::parse::parse::*;
|
|
||||||
use logos::Logos;
|
|
||||||
|
|
||||||
pub mod parse;
|
|
||||||
|
|
||||||
macro_rules! arrow {
|
|
||||||
($spaces:expr) => {
|
|
||||||
println!("{}↓", $spaces);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// let fc = fasm_codegen!(
|
|
||||||
// vec![
|
|
||||||
// Expr::VarDefinition(VarDefinition {name: "goren", value: Value::Number(10)}),
|
|
||||||
// Expr::MathExpr(Math {
|
|
||||||
// left: &Value::Var(VarReference { name: "goren"}),
|
|
||||||
// right: &Value::Number(17),
|
|
||||||
// operator: MathOperator::OP_MULT
|
|
||||||
// }
|
|
||||||
// ),
|
|
||||||
// Expr::FunDefinition(FunDefinition {
|
|
||||||
// name: "adder", contents: vec![
|
|
||||||
// Expr::MathExpr(
|
|
||||||
// Math {
|
|
||||||
// left: &Value::Param(ParamReference {param_number: 0}),
|
|
||||||
// right: &Value::Param(ParamReference {param_number: 1}),
|
|
||||||
// operator: MathOperator::OP_ADD
|
|
||||||
// }
|
|
||||||
// )
|
|
||||||
// ]
|
|
||||||
// }),
|
|
||||||
|
|
||||||
// Expr::FunCall(
|
|
||||||
// FunCall {
|
|
||||||
// name: "adder",
|
|
||||||
// params: vec![Value::Var(VarReference {name: "goren"}), Value::Number(6)]
|
|
||||||
// }
|
|
||||||
// ),
|
|
||||||
|
|
||||||
// Expr::Breakpoint
|
|
||||||
// ]
|
|
||||||
// );
|
|
||||||
|
|
||||||
|
|
||||||
// println!("{}", fc);
|
|
||||||
let parsed = "30 * 60";
|
|
||||||
|
|
||||||
let mut lexer = Token::lexer(parsed);
|
|
||||||
|
|
||||||
println!("\"{}\"", parsed);
|
|
||||||
arrow!(" ");
|
|
||||||
println!("{:?}", lex_str(parsed));
|
|
||||||
arrow!(" ");
|
|
||||||
let parsed = parse_math(lexer);
|
|
||||||
println!("{:?}", parsed);
|
|
||||||
arrow!(" ");
|
|
||||||
println!("{}", fasm_codegen!(&vec![parsed.unwrap()]));
|
|
||||||
}
|
|
|
@ -1,234 +1,249 @@
|
||||||
use crate::parse::ast::*;
|
use crate::parse::ast::*;
|
||||||
use std::rc::Rc;
|
|
||||||
use skylang::temp;
|
use skylang::temp;
|
||||||
|
|
||||||
#[macro_export]
|
const REGISTERS: [&str; 9] = ["r10", "r11", "r12", "r13", "r14", "r15", "rax", "rdi", "rsi"];
|
||||||
macro_rules! fasm_codegen {
|
|
||||||
// Macro to make calling fasm_codegen function easier.
|
|
||||||
($exprs:expr) => {
|
|
||||||
fasm_codegen($exprs, true)
|
|
||||||
};
|
|
||||||
|
|
||||||
(fun: $exprs:expr) => {
|
pub struct FasmCodegen {
|
||||||
fasm_codegen($exprs, false)
|
register_counter: usize
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
|
impl FasmCodegen {
|
||||||
// Define asm_func, used for functions.
|
pub fn new() -> Self {
|
||||||
let mut asm_func = String::new();
|
FasmCodegen {
|
||||||
// Define asm_data, used for variables.
|
register_counter: 0,
|
||||||
let mut asm_data = String::new();
|
|
||||||
// Define asm_start, used for the entry point.
|
|
||||||
let mut asm_start = String::new();
|
|
||||||
macro_rules! unwrap {
|
|
||||||
($item:expr) => {
|
|
||||||
asm_start.push_str(fasm_codegen!(fun: &vec![$item.as_ref().clone()]).as_str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn fasm_codegen(&mut self, exprs: &Vec<Expr>, not_a_function: bool) -> String {
|
||||||
|
macro_rules! fasm_codegen {
|
||||||
|
// Macro to make calling fasm_codegen function easier.
|
||||||
|
($exprs:expr) => {{
|
||||||
|
self.fasm_codegen($exprs, true)
|
||||||
|
}};
|
||||||
|
|
||||||
// If not_a_function, push necessary headers to the asm_start variable.
|
(fun: $exprs:expr) => {{
|
||||||
if not_a_function {
|
self.fasm_codegen($exprs, false)
|
||||||
asm_start.push_str("format ELF64 executable 3\n");
|
}};
|
||||||
asm_start.push_str("segment readable executable\n");
|
}
|
||||||
asm_start.push_str("entry _start\n");
|
|
||||||
asm_start.push_str("_start:\n");
|
|
||||||
asm_data.push_str("\nsegment readable writable\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate over expressions.
|
// Define asm_func, used for functions.
|
||||||
for expr in exprs.iter() {
|
let mut asm_func = String::new();
|
||||||
// Use patern matching on `expr`.
|
// Define asm_data, used for variables.
|
||||||
match expr {
|
let mut asm_data = String::new();
|
||||||
// If the expression is a math expression.
|
// Define asm_start, used for the entry point.
|
||||||
Expr::MathExpr(e) => {
|
let mut asm_start = String::new();
|
||||||
unwrap!(e.left);
|
macro_rules! unwrap {
|
||||||
asm_start.push_str(format!("\tmov r10, rax\n").as_str());
|
($item:expr) => {
|
||||||
unwrap!(e.right);
|
asm_start.push_str(fasm_codegen!(fun: &vec![$item.as_ref().clone()]).as_str());
|
||||||
asm_start.push_str(format!("\tmov r11, rax\n").as_str());
|
}
|
||||||
match e.operator {
|
}
|
||||||
// If the operator is addition.
|
|
||||||
MathOperator::OP_ADD => {
|
|
||||||
asm_start.push_str("\tadd r10, r11\n");
|
|
||||||
asm_start.push_str("\tmov rax, r10\n");
|
|
||||||
// r10 ← r10 + r11; rax ← r10;
|
|
||||||
// The sum will now be stored in the %rax register.
|
|
||||||
},
|
|
||||||
// If the operator is multiplication.
|
|
||||||
MathOperator::OP_MULT => {
|
|
||||||
asm_start.push_str("\timul r10, r11\n");
|
|
||||||
asm_start.push_str("\tmov rax, r10\n");
|
|
||||||
// r10 ← r10 * r11; rax ← r10;
|
|
||||||
// The product will now be stored in the %rax register.
|
|
||||||
},
|
|
||||||
// If the operator is division.
|
|
||||||
MathOperator::OP_DIV => {
|
|
||||||
asm_start.push_str("\tmov rax, r10\n");
|
|
||||||
asm_start.push_str("\txor rdx, rdx\n");
|
|
||||||
asm_start.push_str("\tidiv r11\n");
|
|
||||||
// rax ← r10; rdx ← 0; rax ← concat(rax, rdx) / r11;
|
|
||||||
// The quotient will now be stored in the %rax register.
|
|
||||||
},
|
|
||||||
// If the operators is subtraction.
|
|
||||||
MathOperator::OP_SUB => {
|
|
||||||
asm_start.push_str("\tsub r10, r11\n");
|
|
||||||
asm_start.push_str("\tmov rax, r10\n");
|
|
||||||
// r10 ← r10 - r11; rax ← r10;
|
|
||||||
// The difference will now be stored in the %rax register.
|
|
||||||
},
|
|
||||||
// If the operator is modulo.
|
|
||||||
MathOperator::OP_MOD => {
|
|
||||||
asm_start.push_str("\tmov rax, r10\n");
|
|
||||||
asm_start.push_str("\txor rdx, rdx\n");
|
|
||||||
asm_start.push_str("\tidiv r11\n");
|
|
||||||
asm_start.push_str("\tmov rax, rdx\n");
|
|
||||||
// rax ← r10; rdx ← 0; rdx ← concat(rax, rdx) % r11; rax ← rdx;
|
|
||||||
// The remainder will now be stored in the %rax register.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// If the expression is a function call.
|
|
||||||
Expr::FunCall(e) => {
|
// If not_a_function, push necessary headers to the asm_start variable.
|
||||||
for (i, p) in e.params.iter().enumerate() {
|
if not_a_function {
|
||||||
match i {
|
asm_start.push_str("format ELF64 executable 3\n");
|
||||||
0 => {
|
asm_start.push_str("segment readable executable\n");
|
||||||
// First parameter. Put in %rdi.
|
asm_start.push_str("entry _start\n");
|
||||||
unwrap!(p);
|
asm_start.push_str("_start:\n");
|
||||||
asm_start.push_str(format!("\tmov rdi, rax\n").as_str());
|
asm_data.push_str("\nsegment readable writable\n");
|
||||||
// rdi ← e.params[0];
|
}
|
||||||
|
|
||||||
|
// Iterate over expressions.
|
||||||
|
for expr in exprs.iter() {
|
||||||
|
// Use patern matching on `expr`.
|
||||||
|
match expr {
|
||||||
|
// If the expression is a math expression.
|
||||||
|
Expr::MathExpr(e) => {
|
||||||
|
unwrap!(e.left);
|
||||||
|
self.register_counter += 1;
|
||||||
|
asm_start.push_str(format!("\tmov {}, rax\n", REGISTERS[self.register_counter]).as_str());
|
||||||
|
unwrap!(e.right);
|
||||||
|
self.register_counter += 1;
|
||||||
|
asm_start.push_str(format!("\tmov {}, rax\n", REGISTERS[self.register_counter]).as_str());
|
||||||
|
match e.operator {
|
||||||
|
// If the operator is addition.
|
||||||
|
MathOperator::OP_ADD => {
|
||||||
|
asm_start.push_str(format!("\tadd {}, {}\n", REGISTERS[self.register_counter - 1], REGISTERS[self.register_counter]).as_str());
|
||||||
|
asm_start.push_str(format!("\tmov rax, {}\n", REGISTERS[self.register_counter - 1]).as_str());
|
||||||
|
// r10 ← r10 + r11; rax ← r10;
|
||||||
|
// The sum will now be stored in the %rax register.
|
||||||
},
|
},
|
||||||
|
// If the operator is multiplication.
|
||||||
1 => {
|
MathOperator::OP_MULT => {
|
||||||
// Second parameter. Put in %rsi.
|
asm_start.push_str(format!("\timul {}, {}\n", REGISTERS[self.register_counter - 1], REGISTERS[self.register_counter]).as_str());
|
||||||
unwrap!(p);
|
asm_start.push_str(format!("\tmov rax, {}\n", REGISTERS[self.register_counter - 1]).as_str());
|
||||||
asm_start.push_str(format!("\tmov rsi, rax\n").as_str());
|
// r10 ← r10 * r11; rax ← r10;
|
||||||
// rsi ← e.params[1];
|
// The product will now be stored in the %rax register.
|
||||||
},
|
},
|
||||||
|
// If the operator is division.
|
||||||
2 => {
|
MathOperator::OP_DIV => {
|
||||||
// Third parameter. Put in %rdx.
|
asm_start.push_str("\tmov rax, r10\n");
|
||||||
unwrap!(p);
|
asm_start.push_str("\txor rdx, rdx\n");
|
||||||
asm_start.push_str(format!("\tmov rdx, rax\n").as_str());
|
asm_start.push_str("\tidiv r11\n");
|
||||||
// rdx ← e.params[2];
|
// rax ← r10; rdx ← 0; rax ← concat(rax, rdx) / r11;
|
||||||
|
// The quotient will now be stored in the %rax register.
|
||||||
},
|
},
|
||||||
|
// If the operators is subtraction.
|
||||||
3 => {
|
MathOperator::OP_SUB => {
|
||||||
// Fourth parameter. Put in %rcx.
|
asm_start.push_str("\tsub r10, r11\n");
|
||||||
unwrap!(p);
|
asm_start.push_str("\tmov rax, r10\n");
|
||||||
asm_start.push_str(format!("\tmov rcx, rax\n").as_str());
|
// r10 ← r10 - r11; rax ← r10;
|
||||||
// rcx ← e.params[3];
|
// The difference will now be stored in the %rax register.
|
||||||
},
|
},
|
||||||
|
// If the operator is modulo.
|
||||||
4 => {
|
MathOperator::OP_MOD => {
|
||||||
// Fifth parameter. Put in %r8.
|
asm_start.push_str("\tmov rax, r10\n");
|
||||||
unwrap!(p);
|
asm_start.push_str("\txor rdx, rdx\n");
|
||||||
asm_start.push_str(format!("\tmov r8, rax").as_str());
|
asm_start.push_str("\tidiv r11\n");
|
||||||
// r8 ← e.params[4];
|
asm_start.push_str("\tmov rax, rdx\n");
|
||||||
},
|
// rax ← r10; rdx ← 0; rdx ← concat(rax, rdx) % r11; rax ← rdx;
|
||||||
|
// The remainder will now be stored in the %rax register.
|
||||||
5 => {
|
|
||||||
// Sixth parameter. Put in %r9.
|
|
||||||
unwrap!(p);
|
|
||||||
asm_start.push_str(format!("\tmov r9, rax\n").as_str());
|
|
||||||
// r9 ← e.params[5];
|
|
||||||
},
|
|
||||||
|
|
||||||
_ => {
|
|
||||||
// Parameters after the sixth parameter are pushed to the stack.
|
|
||||||
unwrap!(p);
|
|
||||||
asm_start.push_str(format!("\tpush rax\n").as_str());
|
|
||||||
// STACK_TOP ← e.params[(6+)];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
// Call the function.
|
// If the expression is a function call.
|
||||||
asm_start.push_str(format!("\tcall {}\n", e.name).as_str());
|
Expr::FunCall(e) => {
|
||||||
},
|
for (i, p) in e.params.iter().enumerate() {
|
||||||
|
match i {
|
||||||
|
0 => {
|
||||||
|
// First parameter. Put in %rdi.
|
||||||
|
unwrap!(p);
|
||||||
|
asm_start.push_str(format!("\tmov rdi, rax\n").as_str());
|
||||||
|
// rdi ← e.params[0];
|
||||||
|
},
|
||||||
|
|
||||||
// Define a global variable.
|
1 => {
|
||||||
Expr::GlobalDefinition(e) => {
|
// Second parameter. Put in %rsi.
|
||||||
// Define a 64-bit global variable.
|
unwrap!(p);
|
||||||
|
asm_start.push_str(format!("\tmov rsi, rax\n").as_str());
|
||||||
|
// rsi ← e.params[1];
|
||||||
|
},
|
||||||
|
|
||||||
asm_data.push_str(format!("\t{} dq {}", e.name, e.value).as_str());
|
2 => {
|
||||||
},
|
// Third parameter. Put in %rdx.
|
||||||
|
unwrap!(p);
|
||||||
|
asm_start.push_str(format!("\tmov rdx, rax\n").as_str());
|
||||||
|
// rdx ← e.params[2];
|
||||||
|
},
|
||||||
|
|
||||||
// Breakpoint.
|
3 => {
|
||||||
Expr::Breakpoint => {
|
// Fourth parameter. Put in %rcx.
|
||||||
// Write the interrupt for a debugger breakpoint.
|
unwrap!(p);
|
||||||
asm_start.push_str("\tint3\n");
|
asm_start.push_str(format!("\tmov rcx, rax\n").as_str());
|
||||||
},
|
// rcx ← e.params[3];
|
||||||
|
},
|
||||||
|
|
||||||
// Return something from a function.
|
4 => {
|
||||||
Expr::Return(e) => {
|
// Fifth parameter. Put in %r8.
|
||||||
// Do the operation that should later be returned.
|
unwrap!(p);
|
||||||
asm_start.push_str(fasm_codegen!(fun: &e).as_str());
|
asm_start.push_str(format!("\tmov r8, rax").as_str());
|
||||||
// Move the return value to rbp + 8.
|
// r8 ← e.params[4];
|
||||||
// [rbp + 8] ← return_value
|
},
|
||||||
},
|
|
||||||
|
|
||||||
// A function defenition.
|
5 => {
|
||||||
Expr::FunDefinition(e) => {
|
// Sixth parameter. Put in %r9.
|
||||||
// In x86-64 assembly, a function is defined as <function_name>:. Push this to the `asm_func`.
|
unwrap!(p);
|
||||||
asm_func.push_str(format!("{}:\n", e.name).as_str());
|
asm_start.push_str(format!("\tmov r9, rax\n").as_str());
|
||||||
// Call the function itself specifying that you are defining a function, and push the returned value to `asm_func`.
|
// r9 ← e.params[5];
|
||||||
asm_func.push_str(fasm_codegen!(fun: &e.contents).as_str());
|
},
|
||||||
// Use the ret instruction to return from the procedure.
|
|
||||||
asm_func.push_str("\tret\n");
|
|
||||||
},
|
|
||||||
|
|
||||||
Expr::If(e) => {
|
_ => {
|
||||||
// Increment the temporary variable/function counter.
|
// Parameters after the sixth parameter are pushed to the stack.
|
||||||
// Compare the left and right value.
|
unwrap!(p);
|
||||||
unwrap!(e.left);
|
asm_start.push_str(format!("\tpush rax\n").as_str());
|
||||||
asm_start.push_str("mov rdi, rax");
|
// STACK_TOP ← e.params[(6+)];
|
||||||
unwrap!(e.right);
|
}
|
||||||
asm_start.push_str("mov rsi, rax");
|
}
|
||||||
asm_start.push_str(format!("\tcmp rdi, rsi\n").as_str());
|
|
||||||
// Check what the condition is.
|
|
||||||
match e.cond {
|
|
||||||
COND_OP::EQ => {
|
|
||||||
// If the compared values are equal to each other jump to the temporary function.
|
|
||||||
asm_start.push_str(format!("je .{}", temp!()).as_str());
|
|
||||||
},
|
|
||||||
|
|
||||||
COND_OP::NE => {
|
|
||||||
// If the compared values are not equal to eachother jump to the temporary function.
|
|
||||||
asm_start.push_str(format!("jne .{}", temp!()).as_str());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Create the temporary function.
|
// Call the function.
|
||||||
asm_func.push_str(format!(".{}:\n", temp!()).as_str());
|
asm_start.push_str(format!("\tcall {}\n", e.name).as_str());
|
||||||
asm_func.push_str(fasm_codegen!(fun: &e.action).as_str());
|
},
|
||||||
asm_func.push_str("\tret\n");
|
|
||||||
|
// Define a global variable.
|
||||||
|
Expr::GlobalDefinition(e) => {
|
||||||
|
// Define a 64-bit global variable.
|
||||||
|
|
||||||
|
asm_data.push_str(format!("\t{} dq {}", e.name, e.value).as_str());
|
||||||
|
},
|
||||||
|
|
||||||
|
// Breakpoint.
|
||||||
|
Expr::Breakpoint => {
|
||||||
|
// Write the interrupt for a debugger breakpoint.
|
||||||
|
asm_start.push_str("\tint3\n");
|
||||||
|
},
|
||||||
|
|
||||||
|
// Return something from a function.
|
||||||
|
Expr::Return(e) => {
|
||||||
|
// Do the operation that should later be returned.
|
||||||
|
asm_start.push_str(fasm_codegen!(fun: &e).as_str());
|
||||||
|
// Move the return value to rbp + 8.
|
||||||
|
// [rbp + 8] ← return_value
|
||||||
|
},
|
||||||
|
|
||||||
|
// A function defenition.
|
||||||
|
Expr::FunDefinition(e) => {
|
||||||
|
// In x86-64 assembly, a function is defined as <function_name>:. Push this to the `asm_func`.
|
||||||
|
asm_func.push_str(format!("{}:\n", e.name).as_str());
|
||||||
|
// Call the function itself specifying that you are defining a function, and push the returned value to `asm_func`.
|
||||||
|
asm_func.push_str(fasm_codegen!(fun: &e.contents).as_str());
|
||||||
|
// Use the ret instruction to return from the procedure.
|
||||||
|
asm_func.push_str("\tret\n");
|
||||||
|
},
|
||||||
|
|
||||||
|
Expr::If(e) => {
|
||||||
|
// Increment the temporary variable/function counter.
|
||||||
|
// Compare the left and right value.
|
||||||
|
unwrap!(e.left);
|
||||||
|
asm_start.push_str("mov rdi, rax");
|
||||||
|
unwrap!(e.right);
|
||||||
|
asm_start.push_str("mov rsi, rax");
|
||||||
|
asm_start.push_str(format!("\tcmp rdi, rsi\n").as_str());
|
||||||
|
// Check what the condition is.
|
||||||
|
match e.cond {
|
||||||
|
COND_OP::EQ => {
|
||||||
|
// If the compared values are equal to each other jump to the temporary function.
|
||||||
|
asm_start.push_str(format!("je .{}", temp!()).as_str());
|
||||||
|
},
|
||||||
|
|
||||||
|
COND_OP::NE => {
|
||||||
|
// If the compared values are not equal to eachother jump to the temporary function.
|
||||||
|
asm_start.push_str(format!("jne .{}", temp!()).as_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the temporary function.
|
||||||
|
asm_func.push_str(format!(".{}:\n", temp!()).as_str());
|
||||||
|
asm_func.push_str(fasm_codegen!(fun: &e.action).as_str());
|
||||||
|
asm_func.push_str("\tret\n");
|
||||||
|
|
||||||
|
},
|
||||||
|
Expr::Number(n) => {
|
||||||
|
asm_func.push_str(format!("\tmov rax, {}\n", n).as_str())
|
||||||
|
},
|
||||||
|
no => unsafe {
|
||||||
|
// Write some data I randomly typed to your memory because don't going around playing with something that I haven't implemented yet.
|
||||||
|
println!("{:?} is not. implemented.", no);
|
||||||
|
let mut ptr = 0x00 as *mut f64;
|
||||||
|
::std::ptr::write(ptr, 124010240120401240.12410240124120401240);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
},
|
|
||||||
Expr::Number(n) => {
|
|
||||||
asm_func.push_str(format!("\tmov rax, {}\n", n).as_str())
|
|
||||||
},
|
|
||||||
no => unsafe {
|
|
||||||
// Write some data I randomly typed to your memory because don't going around playing with something that I haven't implemented yet.
|
|
||||||
println!("{:?} is not. implemented.", no);
|
|
||||||
let mut ptr = 0x00 as *mut f64;
|
|
||||||
::std::ptr::write(ptr, 124010240120401240.12410240124120401240);
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if not_a_function {
|
||||||
|
// Use the exit syscall to leave the program. If you don't do this, you will get a segmentation fault.
|
||||||
|
asm_start.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n");
|
||||||
|
asm_start.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n");
|
||||||
|
asm_start.push_str("\tsyscall ; this is the instruction to actually perform the system call.\n");
|
||||||
|
}
|
||||||
|
// Get the final `asm` string derived from all of the other strings that we have manipulated (finally!).
|
||||||
|
let asm = format!("{}{}{}", asm_start, asm_func, asm_data);
|
||||||
|
// Return the final `asm` string.
|
||||||
|
|
||||||
|
asm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if not_a_function {
|
|
||||||
// Use the exit syscall to leave the program. If you don't do this, you will get a segmentation fault.
|
|
||||||
asm_start.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n");
|
|
||||||
asm_start.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n");
|
|
||||||
asm_start.push_str("\tsyscall ; this is the instruction to actually perform the system call.\n");
|
|
||||||
}
|
|
||||||
// Get the final `asm` string derived from all of the other strings that we have manipulated (finally!).
|
|
||||||
let asm = format!("{}{}{}", asm_start, asm_func, asm_data);
|
|
||||||
// Return the final `asm` string.
|
|
||||||
|
|
||||||
asm
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#![feature(associated_type_bounds)]
|
||||||
#![allow(warnings)]
|
#![allow(warnings)]
|
||||||
|
|
||||||
pub mod lex;
|
pub mod lex;
|
||||||
|
@ -51,7 +52,7 @@ fn main() {
|
||||||
|
|
||||||
|
|
||||||
// println!("{}", fc);
|
// println!("{}", fc);
|
||||||
let parsed = "30 * 60";
|
let parsed = "3*10+5";
|
||||||
|
|
||||||
let mut lexer = Token::lexer(parsed);
|
let mut lexer = Token::lexer(parsed);
|
||||||
|
|
||||||
|
@ -62,6 +63,6 @@ fn main() {
|
||||||
let parsed = parse_math(lexer);
|
let parsed = parse_math(lexer);
|
||||||
println!("{:?}", parsed);
|
println!("{:?}", parsed);
|
||||||
arrow!(" ");
|
arrow!(" ");
|
||||||
println!("{}", fasm_codegen!(&vec![parsed.unwrap()]));
|
println!("{}", FasmCodegen::new().fasm_codegen(&vec![parsed.unwrap()], true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,27 +10,18 @@ macro_rules! unwrap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! parse_value {
|
|
||||||
($parse:expr) => {
|
|
||||||
parse_value(&($parse.next(), $parse.slice()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_math(mut tokens: Lexer<Token>) -> Option<Expr> {
|
pub fn parse_math(mut tokens: Lexer<Token>) -> Option<Expr> {
|
||||||
// Is it a Value? → Is it an operator? → Is it a value?
|
// Is it a Value? → Is it an operator? → Is it a value?
|
||||||
|
|
||||||
if let Some(left) = parse_value!(tokens) {
|
if let Some(Ok(Number(left))) = tokens.next() {
|
||||||
if let Some(operator) = match_operator(&mut tokens) {
|
if let Some(op) = match_operator(&mut tokens) {
|
||||||
if let Some(right) = parse_value!(tokens) {
|
if let Some(right) = parse_math(tokens) {
|
||||||
let left = Rc::new(left);
|
return Some(Expr::MathExpr(Math {left: Rc::new(Expr::Number(left)), right: Rc::new(right), operator: op}));
|
||||||
let right = Rc::new(right);
|
|
||||||
|
|
||||||
return Some(Expr::MathExpr(Math {left: left, right: right, operator}))
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return Some(Expr::Number(left));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,50 +43,17 @@ pub fn parse_global_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
|
||||||
tok
|
tok
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_value<'a>(token: &(Option<Result<Token, ()>>, &'a str)) -> Option<Expr<'a>> {
|
|
||||||
if let Some(Ok(tt)) = &token.0 {
|
|
||||||
let mut value = None;
|
|
||||||
|
|
||||||
if let Number(n) = tt {
|
|
||||||
value = Some(Expr::Number(*n));
|
|
||||||
} else if *tt == Identifier {
|
|
||||||
value = Some(Expr::Var(VarReference { name: token.1 }));
|
|
||||||
}
|
|
||||||
|
|
||||||
value
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_fun_call(mut tokens: Lexer<Token>) -> Option<Expr> {
|
|
||||||
// Is it an Ident? → Is it a LeftParen? → Is it a value (I should really make a function to parse that) or is it a RightParen?
|
|
||||||
// ↓ ↓
|
|
||||||
// If it's a value, push that to `params`. Otherwise, params will just be a `Vec::new()`.
|
|
||||||
let mut tok = None;
|
|
||||||
|
|
||||||
if unwrap!(tokens) == Identifier {
|
|
||||||
let name = tokens.slice();
|
|
||||||
if unwrap!(tokens) == LeftParen {
|
|
||||||
let mut params = Vec::new();
|
|
||||||
while let Some(value) = parse_value!(tokens) {
|
|
||||||
params.push(Rc::new(value));
|
|
||||||
}
|
|
||||||
tok = Some(Expr::FunCall(FunCall {name, params: params.clone()}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tok
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn match_operator(tokens: &mut Lexer<Token>) -> Option<MathOperator> {
|
pub fn match_operator(tokens: &mut Lexer<Token>) -> Option<MathOperator> {
|
||||||
match unwrap!(tokens) {
|
if let Some(Ok(token)) = tokens.next() {
|
||||||
Plus => Some(MathOperator::OP_ADD),
|
return match token {
|
||||||
Minus => Some(MathOperator::OP_SUB),
|
Plus => Some(MathOperator::OP_ADD),
|
||||||
Slash => Some(MathOperator::OP_DIV),
|
Minus => Some(MathOperator::OP_SUB),
|
||||||
Star => Some(MathOperator::OP_MULT),
|
Slash => Some(MathOperator::OP_DIV),
|
||||||
Percent => Some(MathOperator::OP_MOD),
|
Star => Some(MathOperator::OP_MULT),
|
||||||
_ => None
|
Percent => Some(MathOperator::OP_MOD),
|
||||||
|
_ => None
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue