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,20 +1,31 @@
|
||||||
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 {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
FasmCodegen {
|
||||||
|
register_counter: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}};
|
||||||
|
|
||||||
|
(fun: $exprs:expr) => {{
|
||||||
|
self.fasm_codegen($exprs, false)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
|
@ -44,21 +55,23 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
|
||||||
// If the expression is a math expression.
|
// If the expression is a math expression.
|
||||||
Expr::MathExpr(e) => {
|
Expr::MathExpr(e) => {
|
||||||
unwrap!(e.left);
|
unwrap!(e.left);
|
||||||
asm_start.push_str(format!("\tmov r10, rax\n").as_str());
|
self.register_counter += 1;
|
||||||
|
asm_start.push_str(format!("\tmov {}, rax\n", REGISTERS[self.register_counter]).as_str());
|
||||||
unwrap!(e.right);
|
unwrap!(e.right);
|
||||||
asm_start.push_str(format!("\tmov r11, rax\n").as_str());
|
self.register_counter += 1;
|
||||||
|
asm_start.push_str(format!("\tmov {}, rax\n", REGISTERS[self.register_counter]).as_str());
|
||||||
match e.operator {
|
match e.operator {
|
||||||
// If the operator is addition.
|
// If the operator is addition.
|
||||||
MathOperator::OP_ADD => {
|
MathOperator::OP_ADD => {
|
||||||
asm_start.push_str("\tadd r10, r11\n");
|
asm_start.push_str(format!("\tadd {}, {}\n", REGISTERS[self.register_counter - 1], REGISTERS[self.register_counter]).as_str());
|
||||||
asm_start.push_str("\tmov rax, r10\n");
|
asm_start.push_str(format!("\tmov rax, {}\n", REGISTERS[self.register_counter - 1]).as_str());
|
||||||
// r10 ← r10 + r11; rax ← r10;
|
// r10 ← r10 + r11; rax ← r10;
|
||||||
// The sum will now be stored in the %rax register.
|
// The sum will now be stored in the %rax register.
|
||||||
},
|
},
|
||||||
// If the operator is multiplication.
|
// If the operator is multiplication.
|
||||||
MathOperator::OP_MULT => {
|
MathOperator::OP_MULT => {
|
||||||
asm_start.push_str("\timul r10, r11\n");
|
asm_start.push_str(format!("\timul {}, {}\n", REGISTERS[self.register_counter - 1], REGISTERS[self.register_counter]).as_str());
|
||||||
asm_start.push_str("\tmov rax, r10\n");
|
asm_start.push_str(format!("\tmov rax, {}\n", REGISTERS[self.register_counter - 1]).as_str());
|
||||||
// r10 ← r10 * r11; rax ← r10;
|
// r10 ← r10 * r11; rax ← r10;
|
||||||
// The product will now be stored in the %rax register.
|
// The product will now be stored in the %rax register.
|
||||||
},
|
},
|
||||||
|
@ -231,4 +244,6 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
|
||||||
// Return the final `asm` string.
|
// Return the final `asm` string.
|
||||||
|
|
||||||
asm
|
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);
|
}
|
||||||
|
} else {
|
||||||
return Some(Expr::MathExpr(Math {left: left, right: right, operator}))
|
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() {
|
||||||
|
return match token {
|
||||||
Plus => Some(MathOperator::OP_ADD),
|
Plus => Some(MathOperator::OP_ADD),
|
||||||
Minus => Some(MathOperator::OP_SUB),
|
Minus => Some(MathOperator::OP_SUB),
|
||||||
Slash => Some(MathOperator::OP_DIV),
|
Slash => Some(MathOperator::OP_DIV),
|
||||||
Star => Some(MathOperator::OP_MULT),
|
Star => Some(MathOperator::OP_MULT),
|
||||||
Percent => Some(MathOperator::OP_MOD),
|
Percent => Some(MathOperator::OP_MOD),
|
||||||
_ => None
|
_ => None
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue