Skip to content

Commit

Permalink
Implement built-in constants (#67)
Browse files Browse the repository at this point in the history
Implement global, built-in constants "pi", "π", "euler", "ℇ", "tau", "τ".

Closes #56
  • Loading branch information
jlapeyre authored Jan 21, 2024
1 parent cb0f97a commit b876609
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 6 additions & 1 deletion crates/oq3_semantics/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// Defines data structures and api for symbols, scope, and symbol tables.

use crate::types;
use crate::types::Type;
use hashbrown::HashMap;

Expand Down Expand Up @@ -237,7 +238,11 @@ impl SymbolTable {
symbol_table_stack: Vec::<SymbolMap>::new(),
all_symbols: Vec::<Symbol>::new(),
};
symbol_table.enter_scope(ScopeType::Global); // May want to initialize with some global symbols as well
symbol_table.enter_scope(ScopeType::Global);
for const_name in ["pi", "π", "euler", "ℇ", "tau", "τ"] {
let _ =
symbol_table.new_binding(const_name, &Type::Float(Some(64), types::IsConst::True));
}
symbol_table
}

Expand Down
4 changes: 3 additions & 1 deletion crates/oq3_semantics/tests/ast_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use oq3_semantics::asg;
use oq3_semantics::symbols;
use oq3_semantics::types;

const NUM_BUILTIN_CONSTS: usize = 6;

//
// TExpr
//
Expand Down Expand Up @@ -88,7 +90,7 @@ fn test_declaration() {
let mut table = SymbolTable::new();
let x = table.new_binding("x", &Type::Bool(IsConst::False));
assert!(x.is_ok());
assert_eq!(table.len_current_scope(), 1);
assert_eq!(table.len_current_scope(), 1 + NUM_BUILTIN_CONSTS);
let result = table.lookup("x");
assert!(result.is_ok());
assert_eq!(result.unwrap().symbol_id(), x.unwrap());
Expand Down
6 changes: 4 additions & 2 deletions crates/oq3_semantics/tests/symbol_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use oq3_semantics::symbols;
use oq3_semantics::types;

const NUM_BUILTIN_CONSTS: usize = 6;

//
// Test API of symbols and symbol tables
//
Expand All @@ -13,7 +15,7 @@ fn test_symbol_table_create() {
use symbols::SymbolTable;

let table = SymbolTable::new();
assert_eq!(table.len_current_scope(), 0);
assert_eq!(table.len_current_scope(), NUM_BUILTIN_CONSTS);
let result = table.lookup("x");
assert!(result.is_err());
}
Expand All @@ -27,7 +29,7 @@ fn test_symbol_table_bind() {
let symbol_name = "x";
let x = table.new_binding(symbol_name, &Type::Bool(IsConst::False));
assert!(x.is_ok());
assert_eq!(table.len_current_scope(), 1);
assert_eq!(table.len_current_scope(), 1 + NUM_BUILTIN_CONSTS);
let result = table.lookup(symbol_name);
assert!(result.is_ok());
assert_eq!(result.unwrap().symbol_id(), x.unwrap());
Expand Down

0 comments on commit b876609

Please sign in to comment.