wip:
Add type system and refator for having only epression
This commit is contained in:
+28
-17
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user