Skip to content

Commit

Permalink
Add: Finish walker and typer (not tested version)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaredanwolfgang committed Dec 6, 2024
1 parent ab582c9 commit a724d31
Show file tree
Hide file tree
Showing 8 changed files with 706 additions and 131 deletions.
4 changes: 4 additions & 0 deletions src/analyser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub enum SemanticError {
id: usize,
message: String,
line: usize,
},
#[error("[Semantic Error] Not Implemented Feature Error: {message:?}")]
NotImplementedFeatureError{
message: String,
},
#[error("System error: {0}")]
SystemError(String),
Expand Down
8 changes: 1 addition & 7 deletions src/analyser/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::{Display, Result};
use crate::symbol::{Symbol, VarType, BasicType, FuncReturnType};
use crate::symbol::{Symbol, VarType, BasicType};
use crate::table::ScopeTable;
use crate::stack::ScopeStack;

Expand Down Expand Up @@ -34,12 +34,6 @@ impl Display for BasicType {
}
}

impl Display for FuncReturnType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result {
write!(f, "{:?}", self)
}
}

impl<T: Clone + std::fmt::Debug> Display for ScopeTable<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut result = String::new();
Expand Down
14 changes: 14 additions & 0 deletions src/analyser/src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use spl_ast::tree::{Value, Variable, CompExpr};
use crate::manager::SymbolManager;
use crate::symbol::*;
use crate::error::SemanticError;
use crate::typer::FuncRetType;

impl From<Value> for BasicType {
fn from(value: Value) -> BasicType {
Expand All @@ -17,3 +18,16 @@ impl From<Value> for BasicType {
}
}
}

impl From<BasicType> for FuncRetType {
fn from(basic_type: BasicType) -> FuncRetType {
match basic_type {
BasicType::Int => FuncRetType::Int,
BasicType::Float => FuncRetType::Float,
BasicType::Char => FuncRetType::Char,
BasicType::Bool => FuncRetType::Bool,
BasicType::String => FuncRetType::String,
BasicType::Null => FuncRetType::Void
}
}
}
30 changes: 17 additions & 13 deletions src/analyser/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl VarSymbol {
manager.new_var_symbol(identifier, VarType::Array((type_t, dimensions)), is_global)
}

pub fn struct_type(manager: &mut SymbolManager, identifier: String, fields: (Vec<String>, Vec<VarType>), is_global: bool) -> VarSymbol {
manager.new_var_symbol(identifier, VarType::Struct(fields), is_global)
pub fn struct_type(manager: &mut SymbolManager, struct_type: String, identifier: String, fields: Vec<(String, VarType)>, is_global: bool) -> VarSymbol {
manager.new_var_symbol(identifier, VarType::Struct((struct_type, fields)), is_global)
}

pub fn get_primitive(&self) -> Option<BasicType> {
Expand All @@ -41,31 +41,35 @@ impl VarSymbol {
}
}

pub fn get_struct_field(&self, field: String) -> Option<BasicType> {
pub fn get_struct_field(&self, field: String) -> Option<VarType> {
match &self.symbol_type {
VarType::Struct((fields, types)) => {
for i in 0..fields.len() {
if fields[i] == field {
return match &types[i] {
VarType::Primitive(t) => Some(*t),
_ => None,
}
VarType::Struct((_ , fields)) => {
for i in fields.iter() {
if i.0 == field {
return Some(i.1.clone());
}
}
None
},
_ => None,
}
}
}

pub fn get_struct_type(&self) -> Option<String> {
match &self.symbol_type {
VarType::Struct((t, _)) => Some(t.clone()),
_ => None,
}
}
}

// From for FuncSymbol
impl FuncSymbol {
fn define(manager: &mut SymbolManager, identifier: String, return_type: FuncReturnType, parameters: Vec<VarType>) -> FuncSymbol {
fn define(manager: &mut SymbolManager, identifier: String, return_type: BasicType, parameters: Vec<VarType>) -> FuncSymbol {
manager.new_func_symbol(identifier, (return_type, parameters), true)
}

fn get_return_type(&self) -> FuncReturnType {
fn get_return_type(&self) -> BasicType {
match &self.symbol_type {
(t, _) => t.clone(),
}
Expand Down
70 changes: 59 additions & 11 deletions src/analyser/src/stack.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use crate::table::ScopeTable;
use crate::symbol::{VarSymbol, FuncSymbol};
use crate::symbol::{VarSymbol, FuncSymbol, VarType, StructType};
use std::process::id;
use std::rc::Rc;
use std::cell::RefCell;
use crate::error::SemanticError;

#[derive(Clone, Debug)]
pub struct ScopeStack {
pub func_scope: Rc<RefCell<ScopeTable<FuncSymbol>>>,
pub struct_scope: Rc<RefCell<ScopeTable<StructType>>>,
pub stack: Vec<Rc<RefCell<ScopeTable<VarSymbol>>>>,
depth: usize,
}

impl ScopeStack {
pub fn new() -> Self {
let func_scope = Rc::new(RefCell::new(ScopeTable::new()));
let struct_scope = Rc::new(RefCell::new(ScopeTable::new()));
let stack = vec![Rc::new(RefCell::new(ScopeTable::new()))];
ScopeStack { func_scope, stack , depth: 0}
ScopeStack { func_scope, struct_scope, stack , depth: 0}
}

// Scope Relevant
Expand Down Expand Up @@ -60,27 +63,46 @@ impl ScopeStack {
}
}

pub fn update_var_symbol(&self, symbol: VarSymbol) -> Result<(), SemanticError> {
// Update the symbol in the top scope
pub fn get_var_symbol(&self, identifier: &String) -> Result<VarSymbol, SemanticError> {
// Search for the symbol in the stack from top to bottom
for scope in self.stack.iter().rev() {
if let Some(current_symbol) = scope.borrow().lookup(&symbol.identifier) {
let mut current_symbol = current_symbol.clone();
current_symbol.symbol_type = symbol.symbol_type.clone();
return Ok(());
if let Some(symbol) = scope.borrow().lookup(identifier) {
return Ok(symbol.clone());
}
}
Err(SemanticError::ReferenceError {
id: 1,
variable: symbol.identifier.clone(),
variable: identifier.clone(),
line: 0,
})
}

pub fn get_var_symbol(&self, identifier: &String) -> Result<VarSymbol, SemanticError> {
pub fn validate_var_symbol(&self, identifier: &String, dim: Vec<usize>) -> Result<VarType, SemanticError> {
// Search for the symbol in the stack from top to bottom
for scope in self.stack.iter().rev() {
if let Some(symbol) = scope.borrow().lookup(identifier) {
return Ok(symbol.clone());
match &symbol.symbol_type {
VarType::Array((_, dimensions)) => {
if dimensions.len() != dim.len() {
return Err(SemanticError::TypeError {
id: 23,
message: "Dimension Mismatched".to_string(),
line: 0,
});
}
return Ok(symbol.symbol_type.clone());
}
_ => {
if dim.len() > 0 {
return Err(SemanticError::TypeError {
id: 10,
message: "Applying indexing operator ([…]) on non-array type variables".to_string(),
line: 0,
});
}
return Ok(symbol.symbol_type.clone());
}
}
}
}
Err(SemanticError::ReferenceError {
Expand All @@ -90,6 +112,32 @@ impl ScopeStack {
})
}

// Struct Relevant
pub fn define_struct(&self, struct_type: StructType) -> Result<(), SemanticError> {
let (identifier, _) = struct_type.clone();
if self.struct_scope.borrow().lookup(&identifier).is_some() {
return Err(SemanticError::RedefinitionError {
id: 15,
variable: identifier.clone(),
line: 0,
});
}else {
self.struct_scope.borrow_mut().insert(identifier.clone(), struct_type);
Ok(())
}
}

pub fn get_struct(&self, type_t: &String) -> Result<StructType, SemanticError> {
if let Some(struct_type) = self.struct_scope.borrow().lookup(type_t) {
return Ok(struct_type.clone());
}
Err(SemanticError::ReferenceError {
id: 14,
variable: type_t.clone(),
line: 0,
})
}

// Function Relevant
pub fn define_func_symbol(&self, symbol: FuncSymbol) -> Result<(), SemanticError> {
if self.func_scope.borrow().lookup(&symbol.identifier).is_some() {
Expand Down
18 changes: 4 additions & 14 deletions src/analyser/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ pub type FuncSymbol = Symbol<FuncType>;
- Array Type `(Vec<usize>, Vec<Value>)`
- Struct Type `(Vec<VarType>)`
- Function Type `FuncType`
- `(FuncReturnType, Vec<VarType>)`
- `(BasicType, Vec<VarType>)`
Different Symbols are stored in different Symbol Tables.
*/

#[derive(Clone, Debug)]
pub enum VarType {
Primitive(PrimType),
Expand All @@ -33,8 +34,8 @@ pub enum VarType {

pub type PrimType = (BasicType);
pub type ArrayType = (BasicType, Vec<usize>);
pub type StructType = (Vec<String>, Vec<VarType>);
pub type FuncType = (FuncReturnType, Vec<VarType>);
pub type StructType = (String, Vec<(String, VarType)>);
pub type FuncType = (BasicType, Vec<VarType>);

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum BasicType {
Expand All @@ -44,15 +45,4 @@ pub enum BasicType {
Bool,
String,
Null
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum FuncReturnType {
Int,
Char,
Float,
Bool,
String,
#[default]
Void
}
Loading

0 comments on commit a724d31

Please sign in to comment.