Skip to content

Commit

Permalink
got generation working
Browse files Browse the repository at this point in the history
  • Loading branch information
JCBurnside committed Mar 19, 2024
1 parent fb69e3b commit 7e93e01
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{"name": "RUST_BACKTRACE", "value": "1"}
],
"console": "externalTerminal",
"preLaunchTask": "rust: cargo build"
"preLaunchTask": "build"
},
{
"name": "test",
Expand All @@ -37,7 +37,7 @@
"cwd": "${workspaceFolder}",
"env": {"RUST_BACKTRACE": "1"},
"console": "externalTerminal",
"preLaunchTask": "rust: cargo build"
"preLaunchTask": "build"
}
]
}
12 changes: 3 additions & 9 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
"version": "2.0.0",
"tasks": [
{
"label": "MyTask",
"label": "build",
"type": "shell",
"command": "cargo build"
},
{
"type": "cargo",
"command": "build",
"command": "cargo build",
"problemMatcher": [
"$rustc"
],
"group": "build",
"label": "rust: cargo build"
}
},
]
}
24 changes: 14 additions & 10 deletions cli/test.fb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
let test_ifs a b : bool -> bool -> int32 =
if a then
print_str "a\n";
Expand All @@ -20,7 +21,9 @@ let test_ifexpr a b : bool -> bool -> int32 = if a then
4

extern "C" let externed : int32 -> int32;

*/
let f (a:[int32;5]) = ();
/*
let show_short_curicuit _ : () -> () =
if (show_something ()) && (show_something_else ()) then
print_str "this is a seperator\n";
Expand All @@ -41,14 +44,15 @@ let show_something_else _ : () -> bool =
let test (a:(int32,int32)) =
let x : int32 = 0;
return x;

*/
let main _ : () -> () =
let a : bool = true;
let x : int32 = 0;
match x where
| 1 -> print_str "one",
| 2 -> print_str "two",
print_str "main";
show_short_curicuit ();
let y = externed 0;
// let a : bool = true;
// let x : int32 = 0;
// match x where
// | 1 -> print_str "one",
// | 2 -> print_str "two",
// print_str "main";
// show_short_curicuit ();
// let y = externed 0;
f [1,2,3,4,5];
return ();
88 changes: 88 additions & 0 deletions compiler/src/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,4 +2405,92 @@ let consume a = fst a;
"produces"
);
}

#[test]
fn array() {
const SRC : &'static str = "
let f (a:[int32;5]) = ();
let main _ : () -> () =
f [1,2,3,4,5];
return ();
";

let ast = crate::Parser::from_source(SRC).module(String::new()).ast;
let dtree = ast.get_dependencies();
let dtree = dtree.into_iter().map(|(key,value)| (key,value.into_iter().collect())).collect();
let mut inference_ctx = super::Context::new(
dtree,
HashMap::new(),
HashMap::new(),
HashMap::new(),
HashMap::new()
);
let mut ast = inference_ctx.inference(ast);
ast.decls.sort_by_key(|decl| decl.get_ident());
let [f,main]=&ast.decls[..] else { unreachable!() };
assert_eq!(
&super::ast::Declaration::Value(super::ast::ValueDeclaration{
loc : (1,4),
is_op:false,
ident:"f".to_string(),
args:vec![
super::ast::ArgDeclaration {
loc:(1,7),
ident:"a".to_string(),
ty : ResolvedType::Array { underlining: types::INT32.boxed(), size: 5 },
id:1,
},
],
ty:ResolvedType::Array { underlining: types::INT32.boxed(), size: 5 }.fn_ty(&types::UNIT),
value:super::ast::ValueType::Expr(super::ast::Expr::UnitLiteral),
generics:None,
abi:None,
id:0
}),
f,
"the function"
);
assert_eq!(
&super::ast::Declaration::Value(super::ast::ValueDeclaration{
loc : (3,4),
is_op:false,
ident:"main".to_string(),
args:vec![
super::ast::ArgDeclaration {
loc:(3,9),
ident:"_".to_string(),
ty : types::UNIT,
id:3,
},
],
ty:types::UNIT.fn_ty(&types::UNIT),
value:super::ast::ValueType::Function(vec![
super::ast::Statement::FnCall(super::ast::FnCall {
loc:(4,4),
value:super::ast::Expr::ValueRead("f".to_string(), (4,4), 10).boxed(),
arg:super::ast::Expr::ArrayLiteral {
contents: vec![
super::ast::Expr::NumericLiteral { value: "1".to_string(), id: 5, ty: types::INT32 },
super::ast::Expr::NumericLiteral { value: "2".to_string(), id: 6, ty: types::INT32 },
super::ast::Expr::NumericLiteral { value: "3".to_string(), id: 7, ty: types::INT32 },
super::ast::Expr::NumericLiteral { value: "4".to_string(), id: 8, ty: types::INT32 },
super::ast::Expr::NumericLiteral { value: "5".to_string(), id: 9, ty: types::INT32 },
],
loc: (4,6),
id: 4
}.boxed(),
id:11,
returns:types::UNIT
}),
super::ast::Statement::Return(super::ast::Expr::UnitLiteral, (5,4))
]),
generics:None,
abi:None,
id:2
}),
main,
"main"
);
}
}
4 changes: 2 additions & 2 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ pub fn from_file<'ctx>(
);
let ast = inference_context.inference(ast);
let mut ast = TypedModuleDeclaration::from(ast, &fwd_declarations, &HashMap::new()); //TODO: foward declare std lib
#[cfg(debug_assertions)]
println!("{:?}", ast.declarations);
// #[cfg(debug_assertions)]
// println!("{:?}", ast.declarations);
ast.lower_generics(&HashMap::new());
(
if errors.is_empty() {
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ where
}

pub fn next_statement(&mut self) -> ParserReturns<Statement> {
match self.stream.clone().next() {
match dbg!(self.stream.clone().next()) {
Some((Token::Let, _)) | Some((Token::For, _)) => {
let ParserReturns {
ast: generics,
Expand Down Expand Up @@ -719,6 +719,7 @@ where
| Token::StringLiteral(_)
| Token::True
| Token::False
| Token::BracketOpen
,_
)) = self.stream.peek() {
match self.stream.peek().map(|(a,_)| a) {
Expand All @@ -736,7 +737,7 @@ where
errors.extend(expr_errors);
}
}
Some(Token::GroupOpen) =>{
Some(Token::GroupOpen | Token::BracketOpen) =>{
let value = self.next_expr();
errors.extend(value.errors);
warnings.extend(value.warnings);
Expand Down
Loading

0 comments on commit 7e93e01

Please sign in to comment.