Compare commits
No commits in common. "6f56ab0b4d46151660f51bfbe201d1fe41742f7f" and "712936e4d6cd8712ca642c361ae4dd635598202f" have entirely different histories.
6f56ab0b4d
...
712936e4d6
@ -30,7 +30,6 @@
|
||||
|
||||
- [ ] Test semantic analysis
|
||||
- [ ] Generate php code from current AST
|
||||
- [x] Typecheck and semantic check simple assignment
|
||||
- [x] Test correct operator precedence
|
||||
- [x] Parse assignments
|
||||
- [x] Parse dot `.` operator
|
||||
|
@ -25,7 +25,6 @@ pub const SEMANTIC_MISMATCHED_TYPES: u32 = 21;
|
||||
pub const SEMANTIC_DUPLICATED_REFERENCE: u32 = 22;
|
||||
pub const SEMANTIC_MISMATCHED_ARGUMENT_COUNT: u32 = 23;
|
||||
pub const SYNTAX_INVALID_ARRAY_ACCESS: u32 = 24;
|
||||
pub const SEMANTIC_IMMUTABLE_VARIABLE: u32 = 25;
|
||||
|
||||
/// Reads the error codes from the error code list
|
||||
pub fn error_code_to_string() -> String {
|
||||
|
@ -38,7 +38,8 @@ pub fn compile_file(input: &String) -> Result<(), ()> {
|
||||
|
||||
let out_code = match compile(&contents) {
|
||||
Ok(out_code) => out_code,
|
||||
Err(_) => {
|
||||
Err(error) => {
|
||||
eprintln!("{}", error);
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
@ -59,15 +60,15 @@ pub fn compile_file(input: &String) -> Result<(), ()> {
|
||||
}
|
||||
|
||||
/// Full pipeline from THP source code to PHP output
|
||||
fn compile(input: &String) -> Result<String, ()> {
|
||||
fn compile(input: &String) -> Result<String, String> {
|
||||
//
|
||||
// Lexical analysis
|
||||
//
|
||||
let tokens = match lexic::get_tokens(input) {
|
||||
Ok(t) => t,
|
||||
Err(error) => {
|
||||
error.print_ariadne(input);
|
||||
return Err(());
|
||||
let chars: Vec<char> = input.chars().collect();
|
||||
return Err(error.get_error_str(&chars));
|
||||
}
|
||||
};
|
||||
|
||||
@ -76,9 +77,9 @@ fn compile(input: &String) -> Result<String, ()> {
|
||||
//
|
||||
let ast = match syntax::build_ast(&tokens) {
|
||||
Ok(ast) => ast,
|
||||
Err(error) => {
|
||||
error.print_ariadne(input);
|
||||
return Err(());
|
||||
Err(reason) => {
|
||||
let chars: Vec<char> = input.chars().collect();
|
||||
return Err(reason.get_error_str(&chars));
|
||||
}
|
||||
};
|
||||
|
||||
@ -88,9 +89,10 @@ fn compile(input: &String) -> Result<String, ()> {
|
||||
let res1 = crate::semantic::check_semantics(&ast);
|
||||
match res1 {
|
||||
Ok(_) => {}
|
||||
Err(error) => {
|
||||
error.print_ariadne(input);
|
||||
return Err(());
|
||||
Err(reason) => {
|
||||
let chars: Vec<char> = input.chars().collect();
|
||||
let error = format!("{}: {}", "error".on_red(), reason.get_error_str(&chars));
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,86 +0,0 @@
|
||||
use crate::{
|
||||
error_handling::{
|
||||
self,
|
||||
error_messages::{SEMANTIC_IMMUTABLE_VARIABLE, SEMANTIC_INVALID_REFERENCE},
|
||||
ErrorContainer, ErrorLabel,
|
||||
},
|
||||
semantic::{self, impls::SemanticCheck, types::Typed},
|
||||
syntax::ast::{Assignment, Positionable},
|
||||
};
|
||||
|
||||
impl SemanticCheck for Assignment<'_> {
|
||||
fn check_semantics(
|
||||
&self,
|
||||
scope: &semantic::symbol_table::SymbolTable,
|
||||
) -> Result<(), error_handling::MistiError> {
|
||||
// for now the assignment can only be to a variable
|
||||
|
||||
// get the datatype and mutability status
|
||||
let datatype = match scope.get_type_and_mut(&self.identifier.value) {
|
||||
Some((datatype, true)) => datatype,
|
||||
Some((_, false)) => {
|
||||
// throw error: variable is immutable
|
||||
let label = ErrorLabel {
|
||||
message: String::from(
|
||||
"This variable is immutable, therefore it cannot be assigned a new value",
|
||||
),
|
||||
start: self.identifier.position,
|
||||
end: self.identifier.get_end_position(),
|
||||
};
|
||||
let econtainer = ErrorContainer {
|
||||
error_code: SEMANTIC_IMMUTABLE_VARIABLE,
|
||||
error_offset: self.identifier.position,
|
||||
labels: vec![label],
|
||||
note: None,
|
||||
help: None,
|
||||
};
|
||||
return Err(econtainer);
|
||||
}
|
||||
None => {
|
||||
// throw error: variable does not exist
|
||||
let label = ErrorLabel {
|
||||
message: String::from("This variable does not exist in this scope"),
|
||||
start: self.identifier.position,
|
||||
end: self.identifier.get_end_position(),
|
||||
};
|
||||
let econtainer = ErrorContainer {
|
||||
error_code: SEMANTIC_INVALID_REFERENCE,
|
||||
error_offset: self.identifier.position,
|
||||
labels: vec![label],
|
||||
note: None,
|
||||
help: None,
|
||||
};
|
||||
return Err(econtainer);
|
||||
}
|
||||
};
|
||||
|
||||
// assert the datatype is the same
|
||||
let expression_type = self.expression.get_type(scope)?;
|
||||
|
||||
if !datatype.equals(&expression_type) {
|
||||
// throw error: variable and expression have different types
|
||||
let label = ErrorLabel {
|
||||
message: format!("This variable has type {:?}", datatype),
|
||||
start: self.identifier.position,
|
||||
end: self.identifier.get_end_position(),
|
||||
};
|
||||
let (expr_start, expr_end) = self.expression.get_position();
|
||||
let label2 = ErrorLabel {
|
||||
message: format!("But this expression has type {:?}", expression_type),
|
||||
start: expr_start,
|
||||
end: expr_end,
|
||||
};
|
||||
let econtainer = ErrorContainer {
|
||||
error_code: SEMANTIC_INVALID_REFERENCE,
|
||||
error_offset: self.identifier.position,
|
||||
labels: vec![label, label2],
|
||||
note: None,
|
||||
help: None,
|
||||
};
|
||||
return Err(econtainer);
|
||||
}
|
||||
|
||||
// ok
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ use crate::{
|
||||
impls::SemanticCheck,
|
||||
types::{Type, Typed},
|
||||
},
|
||||
syntax::ast::{var_binding::VariableBinding, Positionable},
|
||||
syntax::ast::var_binding::VariableBinding,
|
||||
};
|
||||
|
||||
impl SemanticCheck for VariableBinding<'_> {
|
||||
@ -46,33 +46,25 @@ impl SemanticCheck for VariableBinding<'_> {
|
||||
|
||||
// Both the declared & actual datatypes must be the same
|
||||
if datatype != expression_datatype {
|
||||
// This can only happen if the binding has an annotated type,
|
||||
// so its safe to unwrap here
|
||||
let datatype_token = self.datatype.unwrap();
|
||||
|
||||
let label1 = ErrorLabel {
|
||||
message: format!("The variable is declared as {:?} here", datatype),
|
||||
start: datatype_token.position,
|
||||
end: datatype_token.get_end_position(),
|
||||
let label = ErrorLabel {
|
||||
message: format!(
|
||||
"The variable `{}` was declared as `{:?}` but its expression has type `{:?}`",
|
||||
binding_name, datatype, expression_datatype
|
||||
),
|
||||
start: self.identifier.position,
|
||||
end: self.identifier.get_end_position(),
|
||||
};
|
||||
let (expr_start, expr_end) = self.expression.get_position();
|
||||
let label2 = ErrorLabel {
|
||||
message: format!("But this expression has type {:?}", expression_datatype),
|
||||
start: expr_start,
|
||||
end: expr_end,
|
||||
};
|
||||
|
||||
let econtainer = ErrorContainer {
|
||||
error_code: SEMANTIC_DUPLICATED_REFERENCE,
|
||||
error_offset: self.identifier.position,
|
||||
labels: vec![label1, label2],
|
||||
labels: vec![label],
|
||||
note: None,
|
||||
help: None,
|
||||
};
|
||||
return Err(econtainer);
|
||||
}
|
||||
|
||||
scope.insert_custom(binding_name.clone(), datatype, self.is_mutable);
|
||||
scope.insert(binding_name.clone(), datatype);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
pub mod assignment;
|
||||
pub mod binding;
|
||||
pub mod block;
|
||||
pub mod conditional;
|
||||
|
@ -25,7 +25,9 @@ impl SemanticCheck for Statement<'_> {
|
||||
Statement::Conditional(c) => c.check_semantics(scope),
|
||||
Statement::ForLoop(f) => f.check_semantics(scope),
|
||||
Statement::WhileLoop(w) => w.check_semantics(scope),
|
||||
Statement::Assignment(a) => a.check_semantics(scope),
|
||||
Statement::Assignment(_assignment) => {
|
||||
unimplemented!("Semantic check for an assignment")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ struct SymbolTableNode {
|
||||
// the parent scope
|
||||
parent: Option<Rc<RefCell<SymbolTableNode>>>,
|
||||
// the current scope
|
||||
scope: HashMap<String, (Type, bool)>,
|
||||
scope: HashMap<String, Type>,
|
||||
}
|
||||
|
||||
impl SymbolTable {
|
||||
@ -33,17 +33,7 @@ impl SymbolTable {
|
||||
|
||||
/// Inserts a new symbol into the current table scope
|
||||
pub fn insert(&self, key: String, value: Type) {
|
||||
self.node.borrow_mut().insert(key, value, false);
|
||||
}
|
||||
|
||||
/// Inserts a new symbol into the current table scope
|
||||
pub fn insert_mutable(&self, key: String, value: Type) {
|
||||
self.node.borrow_mut().insert(key, value, true);
|
||||
}
|
||||
|
||||
/// Inserts a new symbol into the current table scope
|
||||
pub fn insert_custom(&self, key: String, value: Type, is_mutable: bool) {
|
||||
self.node.borrow_mut().insert(key, value, is_mutable);
|
||||
self.node.borrow_mut().insert(key, value);
|
||||
}
|
||||
|
||||
/// Tests if a symbol is declared in the current or parent scopes
|
||||
@ -55,11 +45,6 @@ impl SymbolTable {
|
||||
pub fn get_type<'a>(&'a self, key: &String) -> Option<Type> {
|
||||
self.node.borrow_mut().get_type(key)
|
||||
}
|
||||
|
||||
/// Gets the datatype of a symbol, if it exists, and if its mutable
|
||||
pub fn get_type_and_mut<'a>(&'a self, key: &String) -> Option<(Type, bool)> {
|
||||
self.node.borrow_mut().get_type_and_mut(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl SymbolTableNode {
|
||||
@ -80,8 +65,8 @@ impl SymbolTableNode {
|
||||
}
|
||||
|
||||
/// Inserts a new symbol into the current scope
|
||||
pub fn insert(&mut self, key: String, value: Type, is_mutable: bool) {
|
||||
self.scope.insert(key, (value, is_mutable));
|
||||
pub fn insert(&mut self, key: String, value: Type) {
|
||||
self.scope.insert(key, value);
|
||||
}
|
||||
|
||||
/// Tests if a symbol is declared in the current or parent scopes
|
||||
@ -102,7 +87,7 @@ impl SymbolTableNode {
|
||||
/// Returns the symbol's datatype
|
||||
pub fn get_type<'a>(&'a mut self, key: &String) -> Option<Type> {
|
||||
// Try to get the type in the current scope
|
||||
if let Some((entry, _)) = self.scope.get(key) {
|
||||
if let Some(entry) = self.scope.get(key) {
|
||||
// TODO: Change to allow other types of datatypes: functions, classes, maps
|
||||
return Some(entry.clone());
|
||||
}
|
||||
@ -116,22 +101,4 @@ impl SymbolTableNode {
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the symbol's datatype and mutability
|
||||
pub fn get_type_and_mut<'a>(&'a mut self, key: &String) -> Option<(Type, bool)> {
|
||||
// Try to get the type in the current scope
|
||||
if let Some((entry, mutable)) = self.scope.get(key) {
|
||||
// TODO: Change to allow other types of datatypes: functions, classes, maps
|
||||
return Some((entry.clone(), *mutable));
|
||||
}
|
||||
|
||||
// Try to get the type in the parent scope
|
||||
match &self.parent {
|
||||
Some(parent) => {
|
||||
parent.as_ref().borrow_mut().get_type_and_mut(key)
|
||||
// parent.get_type(key)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user