Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/lean_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn compile_program(program: &str) -> (Bytecode, BTreeMap<usize, String>) {
let intermediate_bytecode = compile_to_intermediate_bytecode(simple_program).unwrap();
// println!("Intermediate Bytecode:\n\n{}", intermediate_bytecode.to_string());
let compiled = compile_to_low_level_bytecode(intermediate_bytecode).unwrap();
// println!("Compiled Program:\n\n{}", compiled.to_string());
println!("Compiled Program:\n\n{}", compiled.to_string());
(compiled, function_locations)
}

Expand Down
11 changes: 8 additions & 3 deletions crates/lean_compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ pub fn parse_program(input: &str) -> Result<(Program, BTreeMap<usize, String>),
match pair.as_rule() {
Rule::constant_declaration => {
let (name, value) = parse_constant_declaration(pair, &constants)?;
constants.insert(name, value);
if constants.insert(name.clone(), value).is_some() {
panic!("Multiply defined constant: {name}");
}
}
Rule::function => {
let location = pair.line_col().0;
let function = parse_function(pair, &constants, &mut trash_var_count)?;
function_locations.insert(location, function.name.clone());
functions.insert(function.name.clone(), function);
let name = function.name.clone();
function_locations.insert(location, name.clone());
if functions.insert(name.clone(), function).is_some() {
panic!("Multiply defined function: {name}");
}
}
Rule::EOI => break,
_ => {}
Expand Down
34 changes: 34 additions & 0 deletions crates/lean_compiler/tests/test_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@ use lean_vm::*;
use p3_symmetric::Permutation;
use utils::{get_poseidon16, get_poseidon24};

#[test]
#[should_panic]
fn test_duplicate_function_name() {
let program = r#"
fn a() -> 1 {
return 0;
}

fn a() -> 1 {
return 1;
}

fn main() {
a();
return;
}
"#;
compile_and_run(program, &[], &[], false);
}

#[test]
#[should_panic]
fn test_duplicate_constant_name() {
let program = r#"
const A = 1;
const A = 0;

fn main() {
return;
}
"#;
compile_and_run(program, &[], &[], false);
}

#[test]
fn test_fibonacci_program() {
// a program to check the value of the 30th Fibonacci number (832040)
Expand Down