Made skylang more recursive.

This commit is contained in:
Goren Barak 2023-11-28 18:00:31 -05:00
parent 0b4e7deda0
commit cb89bc0e9a
3 changed files with 72 additions and 58 deletions

View file

@ -1,4 +1,5 @@
use crate::parse::ast::*; use crate::parse::ast::*;
use std::rc::Rc;
#[macro_export] #[macro_export]
macro_rules! fasm_codegen { macro_rules! fasm_codegen {
@ -27,6 +28,13 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
// Define asm_start, used for the entry point. // Define asm_start, used for the entry point.
let mut asm_start = String::new(); let mut asm_start = String::new();
macro_rules! unwrap {
($item:expr) => {
asm_start.push_str(fasm_codegen!(fun: &vec![*$item.as_ref()]).as_str());-
}
}
// If not_a_function, push necessary headers to the asm_start variable. // If not_a_function, push necessary headers to the asm_start variable.
if not_a_function { if not_a_function {
asm_start.push_str("format ELF64 executable 3\n"); asm_start.push_str("format ELF64 executable 3\n");
@ -42,8 +50,10 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
match expr { match expr {
// If the expression is a math expression. // If the expression is a math expression.
Expr::MathExpr(e) => { Expr::MathExpr(e) => {
asm_start.push_str(format!("\tmov r10, {}\n", e.left.unwrap()).as_str()); unwrap!(e.left);
asm_start.push_str(format!("\tmov r11, {}\n", e.right.unwrap()).as_str()); asm_start.push_str(format!("\tmov r10, rax\n").as_str());
unwrap!(e.right);
asm_start.push_str(format!("\tmov r11, rax").as_str());
match e.operator { match e.operator {
// If the operator is addition. // If the operator is addition.
MathOperator::OP_ADD => { MathOperator::OP_ADD => {
@ -91,43 +101,51 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
for (i, p) in e.params.iter().enumerate() { for (i, p) in e.params.iter().enumerate() {
match i { match i {
0 => { 0 => {
// First parameter. Put in %rdi.← asm_start.push_str(format!("\tmov rdi, {}\n", p.unwrap()).as_str()); // First parameter. Put in %rdi.
unwrap!(p);
asm_start.push_str(format!("\tmov rdi, rax\n").as_str());
// rdi ← e.params[0]; // rdi ← e.params[0];
}, },
1 => { 1 => {
// Second parameter. Put in %rsi. // Second parameter. Put in %rsi.
asm_start.push_str(format!("\tmov rsi, {}\n", p.unwrap()).as_str()); unwrap!(p);
asm_start.push_str(format!("\tmov rsi, rax\n").as_str());
// rsi ← e.params[1]; // rsi ← e.params[1];
}, },
2 => { 2 => {
// Third parameter. Put in %rdx. // Third parameter. Put in %rdx.
asm_start.push_str(format!("\tmov rdx, {}\n", p.unwrap()).as_str()); unwrap!(p);
asm_start.push_str(format!("\tmov rdx, rax\n").as_str());
// rdx ← e.params[2]; // rdx ← e.params[2];
}, },
3 => { 3 => {
// Fourth parameter. Put in %rcx. // Fourth parameter. Put in %rcx.
asm_start.push_str(format!("\tmov rcx, {}\n", p.unwrap()).as_str()); unwrap!(p);
asm_start.push_str(format!("\tmov rcx, rax\n").as_str());
// rcx ← e.params[3]; // rcx ← e.params[3];
}, },
4 => { 4 => {
// Fifth parameter. Put in %r8. // Fifth parameter. Put in %r8.
asm_start.push_str(format!("\tmov r8, {}\n", p.unwrap()).as_str()); unwrap!(p);
asm_start.push_str(format!("\tmov r8, rax").as_str());
// r8 ← e.params[4]; // r8 ← e.params[4];
}, },
5 => { 5 => {
// Sixth parameter. Put in %r9. // Sixth parameter. Put in %r9.
asm_start.push_str(format!("\tmov r9, {}\n", p.unwrap()).as_str()); unwrap!(p);
asm_start.push_str(format!("\tmov r9, rax\n").as_str());
// r9 ← e.params[5]; // r9 ← e.params[5];
}, },
_ => { _ => {
// Parameters after the sixth parameter are pushed to the stack. // Parameters after the sixth parameter are pushed to the stack.
asm_start.push_str(format!("\tpush {}\n", p.unwrap()).as_str()); unwrap!(p);
asm_start.push_str(format!("\tpush rax\n").as_str());
// STACK_TOP ← e.params[(6+)]; // STACK_TOP ← e.params[(6+)];
} }
} }
@ -138,9 +156,10 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
}, },
// Define a global variable. // Define a global variable.
Expr::VarDefinition(e) => { Expr::GlobalDefinition(e) => {
// Define a 64-bit variable. // Define a 64-bit global variable.
asm_data.push_str(format!("\t{} dq {}", e.name, e.value.unwrap()).as_str());
asm_data.push_str(format!("\t{} dq {}", e.name, e.value).as_str());
}, },
// Breakpoint. // Breakpoint.
@ -173,7 +192,8 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
// Increment the temporary variable/function counter. // Increment the temporary variable/function counter.
tmp_counter += 1; tmp_counter += 1;
// Compare the left and right value. // Compare the left and right value.
asm_start.push_str(format!("\tcmp {}, {}\n", e.left.unwrap(), e.right.unwrap()).as_str());
asm_start.push_str(format!("\tcmp rax, {}\n", e.left, e.right.unwrap()).as_str());
// Check what the condition is. // Check what the condition is.
match e.cond { match e.cond {
COND_OP::EQ => { COND_OP::EQ => {

View file

@ -4,10 +4,13 @@ use std::rc::Rc;
pub enum Expr<'a> { pub enum Expr<'a> {
MathExpr(Math<'a>), MathExpr(Math<'a>),
FunCall(FunCall<'a>), FunCall(FunCall<'a>),
FunDefinition(FunDefinition<'a>), FunDefinition(Rc<FunDefinition<'a>>),
VarDefinition(VarDefinition<'a>), GlobalDefinition(Rc<VarDefinition<'a>>),
Return(Vec<Expr<'a>>), Return(Vec<Expr<'a>>),
If(IfCondition<'a>), If(Rc<IfCondition<'a>>),
Var(VarReference<'a>),
Param(ParamReference),
Number(u64),
Breakpoint Breakpoint
} }
@ -16,8 +19,8 @@ pub enum Expr<'a> {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Math<'a> { pub struct Math<'a> {
pub left: Rc<Value<'a>>, pub left: Rc<Expr<'a>>,
pub right: Rc<Value<'a>>, pub right: Rc<Expr<'a>>,
pub operator: MathOperator pub operator: MathOperator
} }
@ -35,7 +38,7 @@ pub enum MathOperator {
#[derive(Debug)] #[derive(Debug)]
pub struct FunCall<'a> { pub struct FunCall<'a> {
pub name: &'a str, pub name: &'a str,
pub params: Vec<Value<'a>>, pub params: Vec<Rc<Expr<'a>>>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -52,7 +55,7 @@ pub struct FunParamDef<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct FunParamCall<'a> { pub struct FunParamCall<'a> {
pub value: Value<'a>, pub value: Expr<'a>,
} }
// VARIABLES // VARIABLES
@ -60,7 +63,7 @@ pub struct FunParamCall<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct VarDefinition<'a> { pub struct VarDefinition<'a> {
pub name: &'a str, pub name: &'a str,
pub value: Value<'a>, pub value: u64,
} }
@ -78,8 +81,8 @@ pub struct ParamReference {
#[derive(Debug)] #[derive(Debug)]
pub struct IfCondition<'a> { pub struct IfCondition<'a> {
pub left: Value<'a>, pub left: Expr<'a>,
pub right: Value<'a>, pub right: Expr<'a>,
pub cond: COND_OP, pub cond: COND_OP,
pub action: Vec<Expr<'a>> pub action: Vec<Expr<'a>>
} }
@ -91,37 +94,28 @@ pub enum COND_OP {
} }
// VALUE // VALUE
// impl<'a> Value<'a> {
// pub fn unwrap(&self) -> String {
// match self {
// Value::Param(e) => {
// match e.param_number {
// 0 => { return "rdi".to_string(); },
// 1 => { return "rsi".to_string(); },
// 2 => { return "rdx".to_string(); },
// 3 => { return "rcx".to_string(); },
// 4 => { return "r8".to_string(); },
// 5 => { return "r9".to_string(); },
// _ => { unimplemented!() }
// }
// },
#[derive(Debug, Copy, Clone)] // Value::Number(e) => {
pub enum Value<'a> { // return e.to_string();
Var(VarReference<'a>), // },
Param(ParamReference),
Number(u64),
}
// Value::Var(e) => {
impl<'a> Value<'a> { // return format!("[{}]", e.name.to_string());
pub fn unwrap(&self) -> String { // },
match self { // }
Value::Param(e) => { // }
match e.param_number { // }
0 => { return "rdi".to_string(); },
1 => { return "rsi".to_string(); },
2 => { return "rdx".to_string(); },
3 => { return "rcx".to_string(); },
4 => { return "r8".to_string(); },
5 => { return "r9".to_string(); },
_ => { unimplemented!() }
}
},
Value::Number(e) => {
return e.to_string();
},
Value::Var(e) => {
return format!("[{}]", e.name.to_string());
},
}
}
}

View file

@ -57,7 +57,7 @@ pub fn parse_var_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
tok tok
} }
pub fn parse_value<'a>(token: &(Option<Result<Token, ()>>, &'a str)) -> Option<Value<'a>> { pub fn parse_value<'a>(token: &(Option<Result<Token, ()>>, &'a str)) -> Option<Expr<'a>> {
if let Some(Ok(tt)) = &token.0 { if let Some(Ok(tt)) = &token.0 {
let mut value = None; let mut value = None;