Merge branch 'recursive'

This commit is contained in:
Goren Barak 2023-11-30 13:01:12 -05:00
commit 1a8d7498e5
8 changed files with 232 additions and 95 deletions

View file

@ -3,6 +3,9 @@ name = "skylang"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[lib]
proc-macro = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]

6
justfile Normal file
View file

@ -0,0 +1,6 @@
run:
cargo run -r
test:
cargo test
build:
cargo build -r

66
src/#main.rs# Normal file
View file

@ -0,0 +1,66 @@
#![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()]));
}

View file

@ -1,10 +1,12 @@
use crate::parse::ast::*; use crate::parse::ast::*;
use std::rc::Rc;
use skylang::temp;
#[macro_export] #[macro_export]
macro_rules! fasm_codegen { macro_rules! fasm_codegen {
// Macro to make calling fasm_codegen function easier. // Macro to make calling fasm_codegen function easier.
($exprs:expr) => { ($exprs:expr) => {
fasm_codegen(&$exprs, true) fasm_codegen($exprs, true)
}; };
(fun: $exprs:expr) => { (fun: $exprs:expr) => {
@ -12,20 +14,19 @@ macro_rules! fasm_codegen {
} }
} }
pub fn temp(counter: u64) -> String {
format!("tmp{:?}", counter)
}
pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String { pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
// A counter for how many temporary variables have been created. This is used to create new ones. The new ones will be called tmp1, tmp2, etc.
let mut tmp_counter: u64 = 0;
// Define asm_func, used for functions. // Define asm_func, used for functions.
let mut asm_func = String::new(); let mut asm_func = String::new();
// Define asm_data, used for variables. // Define asm_data, used for variables.
let mut asm_data = String::new(); let mut asm_data = String::new();
// 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().clone()]).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 {
@ -35,15 +36,17 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
asm_start.push_str("_start:\n"); asm_start.push_str("_start:\n");
asm_data.push_str("\nsegment readable writable\n"); asm_data.push_str("\nsegment readable writable\n");
} }
// Iterate over expressions. // Iterate over expressions.
for expr in exprs.iter() { for expr in exprs.iter() {
// Use patern matching on `expr`. // Use patern matching on `expr`.
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\n").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 +94,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,16 +149,16 @@ 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.
Expr::Breakpoint => { Expr::Breakpoint => {
// Write the interrupt for a debugger breakpoint. // Write the interrupt for a debugger breakpoint.
asm_start.push_str("\tint3\n"); asm_start.push_str("\tint3\n");
asm_start.push_str("\txor rax, rax\n");
}, },
// Return something from a function. // Return something from a function.
@ -155,8 +166,7 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
// Do the operation that should later be returned. // Do the operation that should later be returned.
asm_start.push_str(fasm_codegen!(fun: &e).as_str()); asm_start.push_str(fasm_codegen!(fun: &e).as_str());
// Move the return value to rbp + 8. // Move the return value to rbp + 8.
asm_start.push_str("mov [rbp + 8], rax"); // [rbp + 8] ← return_value
// 8(%rbp) ← return_value
}, },
// A function defenition. // A function defenition.
@ -171,37 +181,42 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
Expr::If(e) => { Expr::If(e) => {
// Increment the temporary variable/function counter. // Increment the temporary variable/function counter.
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()); 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. // Check what the condition is.
match e.cond { match e.cond {
COND_OP::EQ => { COND_OP::EQ => {
// If the compared values are equal to each other jump to the temporary function. // If the compared values are equal to each other jump to the temporary function.
asm_start.push_str(format!("je .{}", temp(tmp_counter)).as_str()); asm_start.push_str(format!("je .{}", temp!()).as_str());
}, },
COND_OP::NE => { COND_OP::NE => {
// If the compared values are not equal to eachother jump to the temporary function. // If the compared values are not equal to eachother jump to the temporary function.
asm_start.push_str(format!("jne .{}", temp(tmp_counter)).as_str()); asm_start.push_str(format!("jne .{}", temp!()).as_str());
} }
} }
// Create the temporary function. // Create the temporary function.
asm_func.push_str(format!(".{}:\n", temp(tmp_counter)).as_str()); asm_func.push_str(format!(".{}:\n", temp!()).as_str());
asm_func.push_str(fasm_codegen!(fun: &e.action).as_str()); asm_func.push_str(fasm_codegen!(fun: &e.action).as_str());
asm_func.push_str("\tret\n"); asm_func.push_str("\tret\n");
} },
Expr::Number(n) => {
_ => unsafe { 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. // 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; let mut ptr = 0x00 as *mut f64;
::std::ptr::write(ptr, 124010240120401240.12410240124120401240); ::std::ptr::write(ptr, 124010240120401240.12410240124120401240);
}, },
} }
} }

View file

@ -51,6 +51,8 @@ pub enum Token {
Fnaf, // fnaf Fnaf, // fnaf
#[token("let")] #[token("let")]
Let, // let Let, // let
#[token("global")]
Global, // global
#[token("if")] #[token("if")]
If, // if If, // if
#[token("else")] #[token("else")]
@ -85,7 +87,7 @@ pub enum Token {
String, // A string literal. String, // A string literal.
#[regex("[0-9]+", |lex| lex.slice().parse().ok())] #[regex("[0-9]+", |lex| lex.slice().parse().ok())]
Number(u64), // An integer. Number(u64), // An integer.
#[regex(r#"[^[0-9]^"^-^[ \t\n\f]^\.^=^(^)^{^}.^,^;]+[^"^-^=^\..^[ \t\n\f]^(^)^{^}^,^;]*"#)] #[regex(r#"[^[0-9]^"^-^[ \t\n\f]^\.^=^(^)^{^}.^,^;^[+-/*%]]+[^"^-^=^\..^[ \t\n\f]^(^)^{^}^,^;^[+-/*%]]*"#)]
Identifier, // An identifier. Identifier, // An identifier.
#[token("true")] #[token("true")]
True, // true True, // true
@ -96,7 +98,6 @@ pub enum Token {
} }
pub fn lex_str(this: &str) -> Vec<(Token, &str)> { pub fn lex_str(this: &str) -> Vec<(Token, &str)> {
println!("\"{}\"", this);
let mut buf = Vec::new(); let mut buf = Vec::new();
let mut lexer = Token::lexer(this); let mut lexer = Token::lexer(this);
while let Some(Ok(token)) = lexer.next() { while let Some(Ok(token)) = lexer.next() {

View file

@ -10,6 +10,12 @@ use logos::Logos;
pub mod parse; pub mod parse;
macro_rules! arrow {
($spaces:expr) => {
println!("{}", $spaces);
}
}
fn main() { fn main() {
// let fc = fasm_codegen!( // let fc = fasm_codegen!(
// vec![ // vec![
@ -45,9 +51,17 @@ fn main() {
// println!("{}", fc); // println!("{}", fc);
let parsed = "hello(hi)"; let parsed = "30 * 60";
let mut lexer = Token::lexer(parsed); let mut lexer = Token::lexer(parsed);
println!("\"{}\"", parsed);
arrow!(" ");
println!("{:?}", lex_str(parsed)); println!("{:?}", lex_str(parsed));
println!("{:?}", parse_fun_call(lexer)); arrow!(" ");
let parsed = parse_math(lexer);
println!("{:?}", parsed);
arrow!(" ");
println!("{}", fasm_codegen!(&vec![parsed.unwrap()]));
} }

View file

@ -1,22 +1,26 @@
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug)] #[derive(Debug, Clone)]
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
} }
// MATH EXPRESSION // MATH EXPRESSION
#[derive(Debug, Copy, Clone)] #[derive(Debug, Clone)]
pub struct Math<'a> { pub struct Math<'a> {
pub left: &'a Value<'a>, pub left: Rc<Expr<'a>>,
pub right: &'a Value<'a>, pub right: Rc<Expr<'a>>,
pub operator: MathOperator pub operator: MathOperator
} }
@ -31,10 +35,10 @@ pub enum MathOperator {
// FUNCTIONS // FUNCTIONS
#[derive(Debug)] #[derive(Debug, Clone)]
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)]
@ -51,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
@ -59,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,
} }
@ -73,17 +77,12 @@ pub struct ParamReference {
pub param_number: u64, pub param_number: u64,
} }
#[derive(Debug, Copy, Clone)] // CONDITIONS
pub enum Value<'a> {
Var(VarReference<'a>),
Param(ParamReference),
Number(u64),
}
#[derive(Debug)] #[derive(Debug)]
pub struct IfCondition<'a> { pub struct IfCondition<'a> {
pub left: Value<'a>, pub left: Rc<Expr<'a>>,
pub right: Value<'a>, pub right: Rc<Expr<'a>>,
pub cond: COND_OP, pub cond: COND_OP,
pub action: Vec<Expr<'a>> pub action: Vec<Expr<'a>>
} }
@ -94,28 +93,29 @@ pub enum COND_OP {
NE, NE,
} }
impl<'a> Value<'a> { // VALUE
pub fn unwrap(&self) -> String { // impl<'a> Value<'a> {
match self { // pub fn unwrap(&self) -> String {
Value::Param(e) => { // match self {
match e.param_number { // Value::Param(e) => {
0 => { return "rdi".to_string(); }, // match e.param_number {
1 => { return "rsi".to_string(); }, // 0 => { return "rdi".to_string(); },
2 => { return "rdx".to_string(); }, // 1 => { return "rsi".to_string(); },
3 => { return "rcx".to_string(); }, // 2 => { return "rdx".to_string(); },
4 => { return "r8".to_string(); }, // 3 => { return "rcx".to_string(); },
5 => { return "r9".to_string(); }, // 4 => { return "r8".to_string(); },
_ => { unimplemented!() } // 5 => { return "r9".to_string(); },
} // _ => { unimplemented!() }
}, // }
// },
Value::Number(e) => { // Value::Number(e) => {
return e.to_string(); // return e.to_string();
}, // },
Value::Var(e) => { // Value::Var(e) => {
return format!("[{}]", e.name.to_string()); // return format!("[{}]", e.name.to_string());
}, // },
} // }
} // }
} // }

View file

@ -1,14 +1,40 @@
use super::ast::*; use super::ast::*;
use crate::lex::tok::*; use crate::lex::tok::*;
use logos::Lexer; use logos::Lexer;
use std::rc::Rc;
#[macro_export]
macro_rules! unwrap { macro_rules! unwrap {
($var:expr) => { ($var:expr) => {
$var.next().unwrap().unwrap() $var.next().unwrap().unwrap()
} }
} }
pub fn parse_var_declaration(mut tokens: Lexer<Token>) -> Option<Expr> { #[macro_export]
macro_rules! parse_value {
($parse:expr) => {
parse_value(&($parse.next(), $parse.slice()))
}
}
pub fn parse_math(mut tokens: Lexer<Token>) -> Option<Expr> {
// Is it a Value? → Is it an operator? → Is it a value?
if let Some(left) = parse_value!(tokens) {
if let Some(operator) = match_operator(&mut tokens) {
if let Some(right) = parse_value!(tokens) {
let left = Rc::new(left);
let right = Rc::new(right);
return Some(Expr::MathExpr(Math {left: left, right: right, operator}))
}
}
}
None
}
pub fn parse_global_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
let mut tok = None; let mut tok = None;
if unwrap!(tokens) == Let { if unwrap!(tokens) == Let {
if unwrap!(tokens) == Identifier { if unwrap!(tokens) == Identifier {
@ -18,12 +44,7 @@ pub fn parse_var_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
if unwrap!(tokens) == Equal { if unwrap!(tokens) == Equal {
let temp_token = unwrap!(tokens); let temp_token = unwrap!(tokens);
if let Number(n) = temp_token { if let Number(n) = temp_token {
let value = Value::Number(n); tok = Some(Expr::GlobalDefinition(Rc::new(VarDefinition {name, value: n})));
println!("{:?}", value);
tok = Some(Expr::VarDefinition(VarDefinition {name, value}));
} else if temp_token == Identifier {
let value = Value::Var(VarReference { name: tokens.slice() });
tok = Some(Expr::VarDefinition(VarDefinition {name, value}));
} }
} }
} }
@ -31,14 +52,14 @@ 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;
if let Number(n) = tt { if let Number(n) = tt {
value = Some(Value::Number(n)); value = Some(Expr::Number(*n));
} else if tt == Identifier { } else if *tt == Identifier {
value = Some(Value::Var(VarReference { name: token.1 })); value = Some(Expr::Var(VarReference { name: token.1 }));
} }
value value
@ -58,8 +79,8 @@ pub fn parse_fun_call(mut tokens: Lexer<Token>) -> Option<Expr> {
let name = tokens.slice(); let name = tokens.slice();
if unwrap!(tokens) == LeftParen { if unwrap!(tokens) == LeftParen {
let mut params = Vec::new(); let mut params = Vec::new();
while let Some(value) = parse_value((tokens.next(), tokens.slice())) { while let Some(value) = parse_value!(tokens) {
params.push(value); params.push(Rc::new(value));
} }
tok = Some(Expr::FunCall(FunCall {name, params: params.clone()})); tok = Some(Expr::FunCall(FunCall {name, params: params.clone()}));
} }
@ -67,3 +88,14 @@ pub fn parse_fun_call(mut tokens: Lexer<Token>) -> Option<Expr> {
tok tok
} }
pub fn match_operator(tokens: &mut Lexer<Token>) -> Option<MathOperator> {
match unwrap!(tokens) {
Plus => Some(MathOperator::OP_ADD),
Minus => Some(MathOperator::OP_SUB),
Slash => Some(MathOperator::OP_DIV),
Star => Some(MathOperator::OP_MULT),
Percent => Some(MathOperator::OP_MOD),
_ => None
}
}