Compare commits

..

No commits in common. "1a8d7498e57145c754fc65fb84f30c961b994b42" and "6e7b2864585246a5197c8d11911b04fba111458f" have entirely different histories.

8 changed files with 95 additions and 232 deletions

View file

@ -3,9 +3,6 @@ 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]

View file

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

View file

@ -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()]));
}

View file

@ -1,12 +1,10 @@
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) => {
@ -14,19 +12,20 @@ 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 {
@ -43,10 +42,8 @@ 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) => {
unwrap!(e.left); asm_start.push_str(format!("\tmov r10, {}\n", e.left.unwrap()).as_str());
asm_start.push_str(format!("\tmov r10, rax\n").as_str()); asm_start.push_str(format!("\tmov r11, {}\n", e.right.unwrap()).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 => {
@ -94,51 +91,43 @@ 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. // First parameter. Put in %rdi.← asm_start.push_str(format!("\tmov rdi, {}\n", p.unwrap()).as_str());
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.
unwrap!(p); asm_start.push_str(format!("\tmov rsi, {}\n", p.unwrap()).as_str());
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.
unwrap!(p); asm_start.push_str(format!("\tmov rdx, {}\n", p.unwrap()).as_str());
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.
unwrap!(p); asm_start.push_str(format!("\tmov rcx, {}\n", p.unwrap()).as_str());
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.
unwrap!(p); asm_start.push_str(format!("\tmov r8, {}\n", p.unwrap()).as_str());
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.
unwrap!(p); asm_start.push_str(format!("\tmov r9, {}\n", p.unwrap()).as_str());
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.
unwrap!(p); asm_start.push_str(format!("\tpush {}\n", p.unwrap()).as_str());
asm_start.push_str(format!("\tpush rax\n").as_str());
// STACK_TOP ← e.params[(6+)]; // STACK_TOP ← e.params[(6+)];
} }
} }
@ -149,16 +138,16 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
}, },
// Define a global variable. // Define a global variable.
Expr::GlobalDefinition(e) => { Expr::VarDefinition(e) => {
// Define a 64-bit global variable. // Define a 64-bit 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.
@ -166,7 +155,8 @@ 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.
// [rbp + 8] ← return_value asm_start.push_str("mov [rbp + 8], rax");
// 8(%rbp) ← return_value
}, },
// A function defenition. // A function defenition.
@ -181,42 +171,37 @@ 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.
unwrap!(e.left); asm_start.push_str(format!("\tcmp {}, {}\n", e.left.unwrap(), e.right.unwrap()).as_str());
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!()).as_str()); asm_start.push_str(format!("je .{}", temp(tmp_counter)).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!()).as_str()); asm_start.push_str(format!("jne .{}", temp(tmp_counter)).as_str());
} }
} }
// Create the temporary function. // Create the temporary function.
asm_func.push_str(format!(".{}:\n", temp!()).as_str()); asm_func.push_str(format!(".{}:\n", temp(tmp_counter)).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) => {
asm_func.push_str(format!("\tmov rax, {}\n", n).as_str()) _ => unsafe {
},
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,8 +51,6 @@ 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")]
@ -87,7 +85,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
@ -98,6 +96,7 @@ 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,12 +10,6 @@ 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![
@ -51,17 +45,9 @@ fn main() {
// println!("{}", fc); // println!("{}", fc);
let parsed = "30 * 60"; let parsed = "hello(hi)";
let mut lexer = Token::lexer(parsed); let mut lexer = Token::lexer(parsed);
println!("\"{}\"", parsed);
arrow!(" ");
println!("{:?}", lex_str(parsed)); println!("{:?}", lex_str(parsed));
arrow!(" "); println!("{:?}", parse_fun_call(lexer));
let parsed = parse_math(lexer);
println!("{:?}", parsed);
arrow!(" ");
println!("{}", fasm_codegen!(&vec![parsed.unwrap()]));
} }

View file

@ -1,26 +1,22 @@
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum Expr<'a> { pub enum Expr<'a> {
MathExpr(Math<'a>), MathExpr(Math<'a>),
FunCall(FunCall<'a>), FunCall(FunCall<'a>),
FunDefinition(Rc<FunDefinition<'a>>), FunDefinition(FunDefinition<'a>),
GlobalDefinition(Rc<VarDefinition<'a>>), VarDefinition(VarDefinition<'a>),
Return(Vec<Expr<'a>>), Return(Vec<Expr<'a>>),
If(Rc<IfCondition<'a>>), If(IfCondition<'a>),
Var(VarReference<'a>),
Param(ParamReference),
Number(u64),
Breakpoint Breakpoint
} }
// MATH EXPRESSION // MATH EXPRESSION
#[derive(Debug, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Math<'a> { pub struct Math<'a> {
pub left: Rc<Expr<'a>>, pub left: &'a Value<'a>,
pub right: Rc<Expr<'a>>, pub right: &'a Value<'a>,
pub operator: MathOperator pub operator: MathOperator
} }
@ -35,10 +31,10 @@ pub enum MathOperator {
// FUNCTIONS // FUNCTIONS
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct FunCall<'a> { pub struct FunCall<'a> {
pub name: &'a str, pub name: &'a str,
pub params: Vec<Rc<Expr<'a>>>, pub params: Vec<Value<'a>>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -55,7 +51,7 @@ pub struct FunParamDef<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct FunParamCall<'a> { pub struct FunParamCall<'a> {
pub value: Expr<'a>, pub value: Value<'a>,
} }
// VARIABLES // VARIABLES
@ -63,7 +59,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: u64, pub value: Value<'a>,
} }
@ -77,12 +73,17 @@ pub struct ParamReference {
pub param_number: u64, pub param_number: u64,
} }
// CONDITIONS #[derive(Debug, Copy, Clone)]
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: Rc<Expr<'a>>, pub left: Value<'a>,
pub right: Rc<Expr<'a>>, pub right: Value<'a>,
pub cond: COND_OP, pub cond: COND_OP,
pub action: Vec<Expr<'a>> pub action: Vec<Expr<'a>>
} }
@ -93,29 +94,28 @@ pub enum COND_OP {
NE, NE,
} }
// VALUE impl<'a> Value<'a> {
// impl<'a> Value<'a> { pub fn unwrap(&self) -> String {
// pub fn unwrap(&self) -> String { match self {
// match self { Value::Param(e) => {
// Value::Param(e) => { match e.param_number {
// match e.param_number { 0 => { return "rdi".to_string(); },
// 0 => { return "rdi".to_string(); }, 1 => { return "rsi".to_string(); },
// 1 => { return "rsi".to_string(); }, 2 => { return "rdx".to_string(); },
// 2 => { return "rdx".to_string(); }, 3 => { return "rcx".to_string(); },
// 3 => { return "rcx".to_string(); }, 4 => { return "r8".to_string(); },
// 4 => { return "r8".to_string(); }, 5 => { return "r9".to_string(); },
// 5 => { return "r9".to_string(); }, _ => { unimplemented!() }
// _ => { 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,40 +1,14 @@
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()
} }
} }
#[macro_export] pub fn parse_var_declaration(mut tokens: Lexer<Token>) -> Option<Expr> {
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 {
@ -44,7 +18,12 @@ pub fn parse_global_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 {
tok = Some(Expr::GlobalDefinition(Rc::new(VarDefinition {name, value: n}))); let value = Value::Number(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}));
} }
} }
} }
@ -52,14 +31,14 @@ 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>> { pub fn parse_value<'a>(token: (Option<Result<Token, ()>>, &'a str)) -> Option<Value<'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(Expr::Number(*n)); value = Some(Value::Number(n));
} else if *tt == Identifier { } else if tt == Identifier {
value = Some(Expr::Var(VarReference { name: token.1 })); value = Some(Value::Var(VarReference { name: token.1 }));
} }
value value
@ -79,8 +58,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) { while let Some(value) = parse_value((tokens.next(), tokens.slice())) {
params.push(Rc::new(value)); params.push(value);
} }
tok = Some(Expr::FunCall(FunCall {name, params: params.clone()})); tok = Some(Expr::FunCall(FunCall {name, params: params.clone()}));
} }
@ -88,14 +67,3 @@ 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
}
}