This commit is contained in:
Shautvast 2025-12-05 20:45:42 +01:00
parent a44024b04a
commit 3037c65f68
10 changed files with 41 additions and 61 deletions

View file

@ -1,4 +1,4 @@
use std::cell::{Ref, RefMut};
use std::cell::{RefMut};
use crate::builtins::{FunctionMap, Signature, add};
use crate::compiler::tokens::TokenType::{DateTime, StringType, Void};
use crate::errors::RuntimeError;
@ -31,7 +31,7 @@ fn print(_self_val: RefMut<Value>, args: Vec<Value>) -> Result<Value, RuntimeErr
}
fn now(_self_val: RefMut<Value>, _args: Vec<Value>) -> Result<Value, RuntimeError> {
Ok(Value::DateTime(Box::new(chrono::DateTime::from(
Ok(Value::DateTime(Box::new(
chrono::Utc::now(),
))))
)))
}

View file

@ -8,28 +8,6 @@ use std::cell::RefMut;
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
macro_rules! mut_list_fn {
(mut $list:ident, mut $args:ident => $body:expr) => {
|self_val: RefMut<Value>, mut $args: Vec<Value>| -> Result<Value, RuntimeError> {
match self_val {
Value::List(mut $list) => $body,
_ => Err(expected_a_list()),
}
}
};
}
macro_rules! list_fn {
($list:ident, $args:ident => $body:expr) => {
|self_val: Ref<Value>, $args: Vec<Value>| -> Result<Value, RuntimeError> {
match self_val {
Value::List($list) => $body,
_ => Err(expected_a_list()),
}
}
};
}
pub(crate) fn list_functions() -> FunctionMap {
let mut list_functions: FunctionMap = HashMap::new();
let functions = &mut list_functions;

View file

@ -2,7 +2,7 @@ mod string;
mod list;
pub(crate) mod globals;
use std::cell::{Ref, RefCell, RefMut};
use std::cell::{RefCell, RefMut};
use crate::builtins::string::string_functions;
use crate::errors::{CompilerError, RuntimeError};
use crate::compiler::tokens::TokenType;

View file

@ -1,4 +1,4 @@
use std::cell::{Ref, RefMut};
use std::cell::{RefMut};
use crate::builtins::{FunctionMap, Parameter, Signature, add, expected};
use crate::errors::RuntimeError;
use crate::compiler::tokens::TokenType::{StringType, U64};

View file

@ -3,7 +3,7 @@ use crate::builtins::lookup;
use crate::compiler::assembly_pass::Op::{
Add, And, Assign, BitAnd, BitOr, BitXor, Call, CallBuiltin, Constant, DefList, DefMap, Divide,
Dup, Equal, Get, Goto, GotoIf, GotoIfNot, Greater, GreaterEqual, Less, LessEqual, ListGet,
Multiply, Negate, Not, NotEqual, Or, Pop, Print, Return, Shr, Subtract,
Multiply, Negate, Not, NotEqual, Or, Pop, Return, Shr, Subtract,
};
use crate::compiler::ast_pass::Expression::{IfElseExpression, IfExpression, NamedParameter};
use crate::compiler::ast_pass::{Expression, Function, Parameter, Statement};
@ -529,7 +529,6 @@ pub enum Op {
Multiply,
Divide,
Negate,
Print,
Return,
Call(usize, usize),
And,

View file

@ -7,7 +7,7 @@ use crate::compiler::tokens::TokenType::{
Bang, Bool, Char, Colon, DateTime, Dot, Else, Eof, Eol, Equal, False, FloatingPoint, Fn, For,
Greater, GreaterEqual, GreaterGreater, Identifier, If, In, Indent, Integer, LeftBrace,
LeftBracket, LeftParen, Less, LessEqual, LessLess, Let, ListType, MapType, Minus, Object, Plus,
Print, Range, RightBrace, RightBracket, RightParen, SingleRightArrow, Slash, Star, StringType,
Range, RightBrace, RightBracket, RightParen, SingleRightArrow, Slash, Star, StringType,
True, U32, U64, Unknown,
};
use crate::compiler::tokens::{Token, TokenType};

View file

@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::fs;
use walkdir::WalkDir;
use crate::{compiler, symbol_builder, AsmRegistry, TIPI_EXT};
use crate::{symbol_builder, AsmRegistry, TIPI_EXT};
use crate::compiler::assembly_pass::AsmChunk;
use crate::errors::TipiLangError;
use crate::errors::TipiLangError::Platform;

View file

@ -9,7 +9,7 @@ use std::collections::HashMap;
use std::fs;
use std::sync::Arc;
use tipi_lang::compiler::assembly_pass::AsmChunk;
use tipi_lang::compiler::{compile, compile_sourcedir, map_underlying, run};
use tipi_lang::compiler::{compile_sourcedir, map_underlying, run};
use tipi_lang::errors::TipiLangError;
use tipi_lang::vm::interpret_async;

View file

@ -1,13 +1,16 @@
use crate::compiler::ast_pass::{Expression, Parameter, Statement};
use crate::builtins::lookup;
use crate::errors::CompilerError;
use crate::errors::CompilerError::{IncompatibleTypes, UndeclaredVariable};
use crate::compiler::tokens::TokenType::{Bool, DateTime, F32, F64, FloatingPoint, Greater, GreaterEqual, I32, I64, Integer, Less, LessEqual, ListType, MapType, Minus, ObjectType, Plus, SignedInteger, StringType, U32, U64, Unknown, UnsignedInteger, Void};
use crate::compiler::ast_pass::{Expression, Parameter, Statement};
use crate::compiler::tokens::TokenType::{
Bool, DateTime, F32, F64, FloatingPoint, Greater, GreaterEqual, I32, I64, Integer, Less,
LessEqual, ListType, MapType, Minus, ObjectType, Plus, SignedInteger, StringType, U32, U64,
Unknown, UnsignedInteger, Void,
};
use crate::compiler::tokens::{Token, TokenType};
use crate::errors::CompilerError;
use crate::errors::CompilerError::IncompatibleTypes;
use log::debug;
use std::collections::HashMap;
use std::ops::Deref;
use crate::compiler::assembly_pass::Op::Assign;
pub enum Symbol {
Function {
@ -57,17 +60,14 @@ pub fn build(path: &str, ast: &[Statement], symbols: &mut HashMap<String, Symbol
},
);
}
Statement::ExpressionStmt { expression } => {
match expression{
Expression::LetExpression { name, var_type, initializer } => {
let key = make_qname(path, name);
symbols.entry(key).or_insert_with(|| Symbol::Variable {
name: name.lexeme.to_string(),
var_type: var_type.clone(),
});
}
_ =>{}
}
Statement::ExpressionStmt {
expression: Expression::LetExpression { name, var_type, .. },
} => {
let key = make_qname(path, name);
symbols.entry(key).or_insert_with(|| Symbol::Variable {
name: name.lexeme.to_string(),
var_type: var_type.clone(),
});
}
_ => {}
}
@ -118,7 +118,10 @@ pub fn calculate_type(
})
}
pub fn infer_type(expr: &Expression, symbols: &HashMap<String, Symbol>) -> Result<TokenType, CompilerError> {
pub fn infer_type(
expr: &Expression,
symbols: &HashMap<String, Symbol>,
) -> Result<TokenType, CompilerError> {
match expr {
Expression::Binary {
left,
@ -219,8 +222,12 @@ pub fn infer_type(expr: &Expression, symbols: &HashMap<String, Symbol>) -> Resul
Expression::MapGet { .. } => Ok(Unknown),
Expression::FieldGet { .. } => Ok(Unknown),
Expression::Range { lower, .. } => infer_type(lower, symbols),
Expression::IfExpression { .. } => Ok(Unknown),
Expression::IfElseExpression { then_branch, else_branch, .. } => {
Expression::IfExpression { .. } => Ok(Unknown),
Expression::IfElseExpression {
then_branch,
else_branch,
..
} => {
let mut then_type = Void;
for statement in then_branch {
if let Statement::ExpressionStmt { expression } = statement {
@ -234,12 +241,14 @@ pub fn infer_type(expr: &Expression, symbols: &HashMap<String, Symbol>) -> Resul
else_type = infer_type(expression, symbols)?
}
}
if then_type != else_type{
Err(CompilerError::IfElseBranchesDoNotMatch(then_type, else_type))
if then_type != else_type {
Err(CompilerError::IfElseBranchesDoNotMatch(
then_type, else_type,
))
} else {
Ok(then_type)
}
},
}
Expression::LetExpression { .. } => Ok(Void),
Expression::ForStatement { .. } => Ok(Void),
}

View file

@ -5,11 +5,10 @@ use crate::compiler::tokens::TokenType;
use crate::errors::{RuntimeError, ValueError};
use crate::value::{Object, Value};
use arc_swap::Guard;
use std::cell::{RefCell, RefMut};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;
use tracing::debug;
pub async fn interpret_async(
registry: Guard<Arc<HashMap<String, AsmChunk>>>,
@ -130,11 +129,6 @@ impl Vm {
Op::Less => binary_op(self, |a, b| Ok(Value::Bool(a < b))),
Op::LessEqual => binary_op(self, |a, b| Ok(Value::Bool(a <= b))),
Op::NotEqual => binary_op(self, |a, b| Ok(Value::Bool(a != b))),
Op::Print => {
debug!("print {:?}", self.stack);
let v = self.pop().borrow().clone();
println!("{}", v);
}
Op::DefList(len) => {
let mut list = vec![];
for _ in 0..*len {