lt-core/src/main.rs

81 lines
2.1 KiB
Rust
Raw Normal View History

2024-05-05 18:19:28 +02:00
use {
std::collections::HashMap,
std::sync::{Arc, RwLock},
2024-05-12 18:58:39 +02:00
std::{boxed::Box, ops::Deref},
2024-05-05 18:19:28 +02:00
};
mod expr;
2024-05-09 20:13:10 +02:00
mod lexer;
mod parser;
2024-05-12 18:58:39 +02:00
mod procedure_compiler;
mod runtime;
mod symbols;
2024-05-05 18:19:28 +02:00
use crate::{
expr::{LTExpr, Statement},
2024-05-12 18:58:39 +02:00
procedure_compiler::ProcedureCompiler,
symbols::Scope,
2024-05-05 18:19:28 +02:00
};
2024-05-12 18:58:39 +02:00
fn compile(
scope: &Arc<RwLock<Scope>>,
name: &str,
2024-05-16 00:47:37 +02:00
source: impl Iterator<Item = char>,
2024-05-12 18:58:39 +02:00
) -> Vec<tisc::assembler::AssemblyWord> {
2024-05-11 00:00:20 +02:00
ProcedureCompiler::new(scope)
.compile(
&parser::parse_expr(
2024-05-12 18:56:10 +02:00
&scope.read().unwrap().typectx,
2024-05-16 00:47:37 +02:00
&mut lexer::LTIRLexer::from(source.peekable())
2024-05-15 23:51:20 +02:00
.filter(|tok| match tok {
(_, Ok(lexer::LTIRToken::Comment(_))) => false,
_ => true
})
.peekable(),
2024-05-12 18:58:39 +02:00
)
.expect("syntax error"),
2024-05-11 00:00:20 +02:00
)
2024-05-11 18:07:58 +02:00
.into_asm(&name.into())
2024-05-11 00:00:20 +02:00
}
2024-05-09 20:13:10 +02:00
2024-05-15 21:40:28 +02:00
/* TODO:
* - Parser error reporting
* - Compiler error reporting
2024-05-16 00:47:37 +02:00
* - write to address resulting from expression
* - sized objects
2024-05-15 21:40:28 +02:00
* - Typecheck for LTExpr::Application
* - typecheck & inference for rest
*/
2024-05-05 18:19:28 +02:00
fn main() {
// create virtual machine with 4096 words of memory
let mut vm = tisc::VM::new(0x1000);
let mut linker = tisc::Linker::new();
let root_scope = crate::runtime::init_runtime(&mut linker);
let main_scope = Scope::with_parent(&root_scope);
let typectx = main_scope.read().unwrap().typectx.clone();
2024-05-05 18:19:28 +02:00
2024-05-16 00:47:37 +02:00
let args: Vec<String> = std::env::args().collect();
let path = &args[1];
let iter_chars = iterate_text::file::characters::IterateFileCharacters::new(path);
/* link assembly-program to symbols
2024-05-05 18:19:28 +02:00
*/
2024-05-16 00:47:37 +02:00
linker.add_procedure("main", compile(&main_scope, "main", iter_chars));
2024-05-11 18:07:58 +02:00
2024-05-16 00:47:37 +02:00
/* load & run compiled bytecode
*/
2024-05-12 18:58:39 +02:00
let main_addr = linker
.get_link_addr(&"main".into())
.expect("'main' not linked");
vm.load(linker.link_total().expect("could not link"));
vm.execute(main_addr);
2024-05-12 18:58:39 +02:00
eprintln!(
"\n====\nVM execution finished\ndatastack = {:?}\n====",
vm.data_stack
);
2024-05-05 18:19:28 +02:00
}