Add type system and refator for having only epression
This commit is contained in:
Giulio Agostini
2026-07-06 10:43:17 +02:00
parent 9f15a00b98
commit 842216729b
23 changed files with 940 additions and 979 deletions
+28 -17
View File
@@ -1,12 +1,12 @@
use core::fmt;
use std::collections::HashMap;
use std::fmt::Display;
use std::ops::{Add, Div, Mul, Not, Rem, Sub};
use crate::backend::environment::Environment;
use crate::common::lox_result::{runtime_error, LoxResult};
use crate::common::types::Type;
use crate::{
backend::environment::EnvironmentStack,
common::ast::{AstNode, Expr, Stmt},
backend::environment::EnvironmentStack, common::ast::AstNode,
frontend::source_registry::SourceSlice,
};
#[derive(Debug, Clone, PartialEq)]
@@ -272,15 +272,20 @@ pub enum BaseValue {
Nil,
Function(LoxFunction),
NativeFunction(NativeFunction),
Struct(Struct),
}
struct Dict {
map: HashMap<String, BaseValue>,
}
struct ReturnValue {
label: Vec<String>,
value: BaseValue,
value: Type,
}
impl ReturnValue {
pub fn new(label: Vec<String>, value: BaseValue) -> Self {
pub fn new(label: Vec<String>, value: Type) -> Self {
Self { label, value }
}
}
@@ -304,39 +309,40 @@ impl Display for BaseValue {
BaseValue::Nil => write!(f, "nil"),
BaseValue::Function(..) => write!(f, "<fn lox function>"),
BaseValue::NativeFunction(..) => write!(f, "<native native function>"),
BaseValue::Struct(_) => write!(f, "<struct>"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LoxFunction {
pub parameters: Vec<(String, String)>,
pub return_type: Option<String>,
pub body: AstNode<Stmt>,
pub parameters: Vec<(String, Type)>,
pub return_type: Type,
pub body: AstNode,
pub closure: Option<EnvironmentStack<BaseValue>>,
pub guard: Option<Box<AstNode<Expr>>>,
pub guard: Option<Box<AstNode>>,
}
impl LoxFunction {
pub fn new(
parameters: Vec<(String, String)>,
body: AstNode<Stmt>,
parameters: Vec<(String, Type)>,
body: AstNode,
closure: Option<EnvironmentStack<BaseValue>>,
guard: Option<Box<AstNode<Expr>>>,
guard: Option<Box<AstNode>>,
) -> Self {
LoxFunction {
parameters,
return_type: None,
return_type: Type::Any,
body,
closure,
guard,
}
}
pub fn anonymous_function(parameters: Vec<(String, String)>, body: AstNode<Stmt>) -> Self {
pub fn anonymous_function(parameters: Vec<(String, Type)>, body: AstNode) -> Self {
LoxFunction {
parameters,
return_type: None,
return_type: Type::Any,
body,
closure: None,
guard: None,
@@ -346,12 +352,12 @@ impl LoxFunction {
#[derive(Debug, Clone, PartialEq)]
pub struct NativeFunction {
pub parameters: Vec<(String, String)>,
pub parameters: Vec<(String, Type)>,
pub function: fn(&[BaseValue]) -> BaseValue,
}
impl NativeFunction {
pub fn new(parameters: Vec<(String, String)>, function: fn(&[BaseValue]) -> BaseValue) -> Self {
pub fn new(parameters: Vec<(String, Type)>, function: fn(&[BaseValue]) -> BaseValue) -> Self {
NativeFunction {
parameters,
function,
@@ -359,6 +365,11 @@ impl NativeFunction {
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Struct {
pub fields: Vec<(String, BaseValue)>,
}
// Trait implementations for BaseValue
pub trait Truthy {