restructuring

This commit is contained in:
Shautvast 2025-11-17 10:58:21 +01:00
parent 5cec67bb01
commit b5d49ed6eb
11 changed files with 45 additions and 50 deletions

View file

@ -1,4 +1,4 @@
use crate::ast_pass::Parameter;
use crate::compiler::ast_pass::Parameter;
use crate::builtins::{FunctionMap, Signature, add, expected};
use crate::errors::RuntimeError;
use crate::tokens::TokenType;

View file

@ -7,7 +7,7 @@ use crate::tokens::TokenType;
use crate::value::Value;
use std::collections::HashMap;
use std::sync::LazyLock;
use crate::ast_pass::Parameter;
use crate::compiler::ast_pass::Parameter;
use crate::builtins::list::list_functions;
pub(crate) struct Signature {

View file

@ -1,4 +1,4 @@
use crate::ast_pass::Parameter;
use crate::compiler::ast_pass::Parameter;
use crate::tokens::TokenType;
use crate::value::Value;
use crate::vm::{

View file

@ -1,4 +1,4 @@
use crate::ast_pass::Expression::{
use crate::compiler::ast_pass::Expression::{
Assignment, FieldGet, FunctionCall, ListGet, MapGet, MethodCall, NamedParameter, Stop, Variable,
};
use crate::errors::CompilerError::{
@ -30,10 +30,10 @@ pub fn compile(
#[derive(Debug, Clone)]
pub struct Function {
pub(crate) name: Token,
pub(crate) parameters: Vec<Parameter>,
pub(crate) return_type: TokenType,
pub(crate) body: Vec<Statement>,
pub name: Token,
pub parameters: Vec<Parameter>,
pub return_type: TokenType,
pub body: Vec<Statement>,
}
struct AstCompiler {
@ -893,12 +893,12 @@ impl Statement {
#[derive(Debug, Clone)]
pub struct Parameter {
pub(crate) name: Token,
pub(crate) var_type: TokenType,
pub name: Token,
pub var_type: TokenType,
}
impl Parameter {
pub(crate) fn new(name: impl Into<String>, value_type: TokenType) -> Self {
pub fn new(name: impl Into<String>, value_type: TokenType) -> Self {
Self {
name: Token {
token_type: TokenType::StringType,

View file

@ -1,5 +1,5 @@
use crate::ast_pass::Expression::NamedParameter;
use crate::ast_pass::{Expression, Function, Parameter, Statement};
use crate::compiler::ast_pass::Expression::NamedParameter;
use crate::compiler::ast_pass::{Expression, Function, Parameter, Statement};
use crate::builtins::lookup;
use crate::chunk::Chunk;
use crate::errors::CompilerError::{IncompatibleTypes, UndeclaredVariable};
@ -22,7 +22,7 @@ pub fn compile(
compile_in_namespace(ast, qualified_name, symbols, registry)
}
pub(crate) fn compile_function(
pub fn compile_function(
function: &Function,
symbols: &SymbolTable,
registry: &mut Registry,
@ -41,7 +41,7 @@ pub(crate) fn compile_function(
Ok(chunk)
}
pub(crate) fn compile_in_namespace(
pub fn compile_in_namespace(
ast: &Vec<Statement>,
namespace: Option<&str>,
symbols: &SymbolTable,
@ -54,7 +54,7 @@ pub(crate) fn compile_in_namespace(
Ok(())
}
pub(crate) struct Compiler {
pub struct Compiler {
chunk: Chunk,
_had_error: bool,
current_line: usize,
@ -62,7 +62,7 @@ pub(crate) struct Compiler {
}
impl Compiler {
pub(crate) fn new(name: &str) -> Self {
pub fn new(name: &str) -> Self {
Self {
chunk: Chunk::new(name),
_had_error: false,
@ -72,7 +72,7 @@ impl Compiler {
}
/// compile the entire AST into a chunk, adding a RETURN OP
pub(crate) fn compile(
pub fn compile(
&mut self,
ast: &Vec<Statement>,
symbols: &SymbolTable,

4
src/compiler/mod.rs Normal file
View file

@ -0,0 +1,4 @@
mod compiler_tests;
pub mod bytecode_pass;
pub mod scan_pass;
pub mod ast_pass;

View file

@ -1,24 +1,20 @@
use crate::ast_pass::{Expression, Statement};
use crate::chunk::Chunk;
use crate::compiler::ast_pass::{Expression, Statement};
use crate::errors::CrudLangError::Platform;
use crate::errors::{CompilerErrorAtLine, CrudLangError};
use crate::scan_pass::scan;
use crate::symbol_builder::Symbol;
use crate::value::Value::Void;
use std::collections::HashMap;
use std::fs;
use walkdir::WalkDir;
use crate::value::Value::Void;
pub mod ast_pass;
mod builtins;
pub mod bytecode_pass;
pub mod chunk;
mod compiler_tests;
pub(crate) mod compiler;
pub mod errors;
pub mod file_watch;
mod keywords;
pub mod repl;
pub mod scan_pass;
mod symbol_builder;
mod tokens;
mod value;
@ -37,19 +33,14 @@ pub fn compile_sourcedir(source_dir: &str) -> Result<HashMap<String, Chunk>, Cru
if path.ends_with(".crud") {
print!("-- Compiling {} -- ", path);
let source = fs::read_to_string(path).map_err(map_underlying())?;
let tokens = scan(&source)?;
let tokens = compiler::scan_pass::scan(&source)?;
let mut symbol_table = HashMap::new();
match ast_pass::compile(Some(path), tokens, &mut symbol_table) {
match compiler::ast_pass::compile(Some(path), tokens, &mut symbol_table) {
Ok(statements) => {
let path = path.strip_prefix(source_dir).unwrap().replace(".crud", "");
symbol_builder::build(&path, &statements, &mut symbol_table);
bytecode_pass::compile(
Some(&path),
&statements,
&symbol_table,
&mut registry,
)?;
compiler::bytecode_pass::compile(Some(&path), &statements, &symbol_table, &mut registry)?;
}
Err(e) => {
println!("{}", e);
@ -67,33 +58,33 @@ pub fn map_underlying() -> fn(std::io::Error) -> CrudLangError {
}
pub fn recompile(src: &str, registry: &mut HashMap<String, Chunk>) -> Result<(), CrudLangError> {
let tokens = scan(src)?;
let tokens = compiler::scan_pass::scan(src)?;
let mut symbol_table = HashMap::new();
let ast = ast_pass::compile(None, tokens, &mut symbol_table)?;
let ast = compiler::ast_pass::compile(None, tokens, &mut symbol_table)?;
symbol_builder::build("", &ast, &mut symbol_table);
bytecode_pass::compile(None, &ast, &symbol_table, registry)?;
compiler::bytecode_pass::compile(None, &ast, &symbol_table, registry)?;
Ok(())
}
pub fn compile(src: &str) -> Result<HashMap<String, Chunk>, CrudLangError> {
let tokens = scan(src)?;
let tokens = compiler::scan_pass::scan(src)?;
let mut registry = HashMap::new();
let mut symbol_table = HashMap::new();
let ast = ast_pass::compile(None, tokens, &mut symbol_table)?;
let ast = compiler::ast_pass::compile(None, tokens, &mut symbol_table)?;
symbol_builder::build("", &ast, &mut symbol_table);
bytecode_pass::compile(None, &ast, &symbol_table, &mut registry)?;
compiler::bytecode_pass::compile(None, &ast, &symbol_table, &mut registry)?;
Ok(registry)
}
#[cfg(test)]
pub(crate) fn run(src: &str) -> Result<value::Value, CrudLangError> {
let tokens = scan(src)?;
let mut symbol_table = HashMap::new();
let ast = ast_pass::compile(None, tokens, &mut symbol_table)?;
symbol_builder::build("", &ast, &mut symbol_table);
let mut registry = HashMap::new();
bytecode_pass::compile(None, &ast, &symbol_table, &mut registry)?;
let tokens = compiler::scan_pass::scan(src)?;
let mut symbol_table = HashMap::new();
let ast = compiler::ast_pass::compile(None, tokens, &mut symbol_table)?;
symbol_builder::build("", &ast, &mut symbol_table);
let mut registry = HashMap::new();
compiler::bytecode_pass::compile(None, &ast, &symbol_table, &mut registry)?;
let registry = arc_swap::ArcSwap::from(std::sync::Arc::new(registry));
vm::interpret(registry.load(), "main").map_err(CrudLangError::from)
let registry = arc_swap::ArcSwap::from(std::sync::Arc::new(registry));
vm::interpret(registry.load(), "main").map_err(CrudLangError::from)
}

View file

@ -1,8 +1,8 @@
use crate::chunk::Chunk;
use crate::errors::CrudLangError;
use crate::scan_pass::scan;
use crate::compiler::scan_pass::scan;
use crate::vm::Vm;
use crate::{ast_pass, bytecode_pass, map_underlying, symbol_builder};
use crate::{compiler::ast_pass, compiler::bytecode_pass, map_underlying, symbol_builder};
use arc_swap::ArcSwap;
use std::collections::HashMap;
use std::io;

View file

@ -1,4 +1,4 @@
use crate::ast_pass::{Expression, Parameter, Statement};
use crate::compiler::ast_pass::{Expression, Parameter, Statement};
use crate::builtins::lookup;
use crate::errors::CompilerError;
use crate::errors::CompilerError::IncompatibleTypes;