Skip to content

Commit

Permalink
cleaned up args def and removed some files. adding a docs section (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeebe4code authored Sep 10, 2023
1 parent f67c90e commit 03f2cef
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
main*

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ members = [
"perror",
"lexer",
"parser",
"obj",
"object",
"linker",
"ast",
"ir",
"e2e",
]
resolver = "2"

4 changes: 2 additions & 2 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct FuncDef {
pub visibility: Option<Lexeme>,
pub mutability: Lexeme,
pub identifier: Box<Expr>,
pub args: Option<Box<Expr>>,
pub args: Option<Vec<Box<Expr>>>,
pub block: Box<Expr>,
}

Expand All @@ -14,7 +14,7 @@ impl FuncDef {
visibility: Option<Lexeme>,
mutability: Lexeme,
identifier: Box<Expr>,
args: Option<Box<Expr>>,
args: Option<Vec<Box<Expr>>>,
block: Box<Expr>,
) -> Self {
FuncDef {
Expand Down
22 changes: 22 additions & 0 deletions docs/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type: Project
name:
details:

---
type: Exe
name: ty
details:
source: ty
targets:
- ref: supported targets

---
type: Target List
name:

---
type: Package
name:



2 changes: 1 addition & 1 deletion e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ lexer = { path = "../lexer" }
token = { path = "../token" }
parser = { path = "../parser" }
ir = { path = "../ir" }
obj = { path = "../obj" }
object = { path = "../object" }
5 changes: 3 additions & 2 deletions e2e/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use ast::*;
use ir::*;
use lexer::*;
use obj::*;
use object::*;
use parser::*;

fn main() {
let lex = ProseLexer::new("pub const main = fn() { return 5; }");
let mut parse = Parser::new(lex);
let ast_parsed = parse.func().unwrap();
let mut ir = FIRSource::new(0);
let mut ir = IRSource::new(0);
match *ast_parsed {
Expr::FuncDef(val) => {
let result = ir.begin(val);
println!("{}", ir.get_ir(&result).unwrap());
build_main(result);
}
_ => panic!("not a func def!"),
Expand Down
3 changes: 0 additions & 3 deletions file.ts

This file was deleted.

20 changes: 6 additions & 14 deletions ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use cranelift_frontend::*;
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use perror::*;

pub struct FIRSource {
pub struct IRSource {
package: u32,
fname: u32,
variables: usize,
}

impl FIRSource {
impl IRSource {
pub fn new(package: u32) -> Self {
FIRSource {
IRSource {
package,
fname: 0,
variables: 0,
Expand All @@ -30,12 +30,10 @@ impl FIRSource {
op: &Block,
builder: &mut FunctionBuilder,
) -> ResultFir<Variable> {
println!("1");
let temp = self.recurse(&op.exprs[0], builder).unwrap();
Ok(temp)
}
pub fn handle_ret(&mut self, op: &RetOp, builder: &mut FunctionBuilder) -> ResultFir<Variable> {
println!("2");
let temp = self.recurse(&op.expr, builder).unwrap();
let arg = builder.use_var(temp);
builder.ins().return_(&[arg]);
Expand All @@ -46,7 +44,6 @@ impl FIRSource {
num: &Number,
builder: &mut FunctionBuilder,
) -> ResultFir<Variable> {
println!("3");
let result = Variable::new(self.variables);
self.variables += 1;
builder.declare_var(result, I64);
Expand All @@ -70,13 +67,8 @@ impl FIRSource {
let name = UserFuncName::user(self.package, self.fname);
// TODO:: put the optional vec of expr directly on funcdef
if let Some(val) = func_def.args {
match *val {
Expr::ArgsDef(arg) => arg
.args
.iter()
.for_each(|_x| sig.params.push(AbiParam::new(I64))),
_ => panic!("developer error, args vec invalid"),
}
val.iter()
.for_each(|_x| sig.params.push(AbiParam::new(I64)));
}
sig.returns.push(AbiParam::new(I64));
let mut func = Function::with_name_signature(name, sig);
Expand Down Expand Up @@ -143,7 +135,7 @@ mod tests {
)]
),
);
let mut fir = FIRSource::new(0);
let mut fir = IRSource::new(0);
let result = fir.begin(func_def);
/*
* function u0:0() -> i64 system_v
Expand Down
8 changes: 8 additions & 0 deletions linker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "linker"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
14 changes: 14 additions & 0 deletions linker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
13 changes: 0 additions & 13 deletions notes.txt

This file was deleted.

2 changes: 1 addition & 1 deletion obj/Cargo.toml → object/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "obj"
name = "object"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
39 changes: 10 additions & 29 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'s> Parser<'s> {
.convert_expr(|span| expr!(TypeSimple, span))
.convert_to_result("expected type".to_string())
}
pub fn args(&mut self) -> ResultOptExpr {
pub fn args(&mut self) -> Result<Option<Vec<Box<Expr>>>> {
if let Some(arg_local) = self.arg() {
let mut arg_list: Vec<Box<Expr>> = vec![];
arg_list.push(arg_local);
Expand All @@ -73,7 +73,7 @@ impl<'s> Parser<'s> {
.expect_expr("expected argument definition".to_string())?,
);
}
return bubble_expr!(ArgsDef, arg_list);
return Ok(Some(arg_list));
}
Ok(None)
}
Expand Down Expand Up @@ -188,10 +188,6 @@ trait ExpectExpr {
fn expect_expr(self, title: String) -> ResultExpr;
}

trait ExpectOkSome {
fn expect_ok_some(self, title: String) -> ResultOptExpr;
}

trait ResultOr {
fn result_or(self, func: impl FnOnce(Box<Expr>) -> ResultExpr) -> ResultExpr;
}
Expand Down Expand Up @@ -225,18 +221,6 @@ impl ResultOr for ResultExpr {
}
}

//impl ExpectOkSome for ResultOptExpr {
// fn expect_ok_some(self, title: String) -> ResultOptExpr {
// match self {
// Err(err) => err,
// Ok(inner) => match inner {
// Some(exp) => exp,
// None => Err(ParseError::new(title)),
// },
// }
// }
//}

impl IfNoneDo for OptExpr {
fn if_none_do(self, func: impl FnOnce() -> OptExpr) -> OptExpr {
match self {
Expand Down Expand Up @@ -502,17 +486,14 @@ mod tests {
span: 10..13
}
),
Some(expr!(
ArgsDef,
vec![expr!(
Symbol,
Lexeme {
slice: "x".to_string(),
token: Token::Symbol,
span: 19..20
}
)]
)),
Some(vec![expr!(
Symbol,
Lexeme {
slice: "x".to_string(),
token: Token::Symbol,
span: 19..20
}
)]),
expr!(
Block,
vec![expr!(
Expand Down

0 comments on commit 03f2cef

Please sign in to comment.