restructuring
This commit is contained in:
parent
5cec67bb01
commit
b5d49ed6eb
11 changed files with 45 additions and 50 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::ast_pass::Parameter;
|
use crate::compiler::ast_pass::Parameter;
|
||||||
use crate::builtins::{FunctionMap, Signature, add, expected};
|
use crate::builtins::{FunctionMap, Signature, add, expected};
|
||||||
use crate::errors::RuntimeError;
|
use crate::errors::RuntimeError;
|
||||||
use crate::tokens::TokenType;
|
use crate::tokens::TokenType;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use crate::tokens::TokenType;
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
use crate::ast_pass::Parameter;
|
use crate::compiler::ast_pass::Parameter;
|
||||||
use crate::builtins::list::list_functions;
|
use crate::builtins::list::list_functions;
|
||||||
|
|
||||||
pub(crate) struct Signature {
|
pub(crate) struct Signature {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::ast_pass::Parameter;
|
use crate::compiler::ast_pass::Parameter;
|
||||||
use crate::tokens::TokenType;
|
use crate::tokens::TokenType;
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
use crate::vm::{
|
use crate::vm::{
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::ast_pass::Expression::{
|
use crate::compiler::ast_pass::Expression::{
|
||||||
Assignment, FieldGet, FunctionCall, ListGet, MapGet, MethodCall, NamedParameter, Stop, Variable,
|
Assignment, FieldGet, FunctionCall, ListGet, MapGet, MethodCall, NamedParameter, Stop, Variable,
|
||||||
};
|
};
|
||||||
use crate::errors::CompilerError::{
|
use crate::errors::CompilerError::{
|
||||||
|
|
@ -30,10 +30,10 @@ pub fn compile(
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub(crate) name: Token,
|
pub name: Token,
|
||||||
pub(crate) parameters: Vec<Parameter>,
|
pub parameters: Vec<Parameter>,
|
||||||
pub(crate) return_type: TokenType,
|
pub return_type: TokenType,
|
||||||
pub(crate) body: Vec<Statement>,
|
pub body: Vec<Statement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AstCompiler {
|
struct AstCompiler {
|
||||||
|
|
@ -893,12 +893,12 @@ impl Statement {
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Parameter {
|
pub struct Parameter {
|
||||||
pub(crate) name: Token,
|
pub name: Token,
|
||||||
pub(crate) var_type: TokenType,
|
pub var_type: TokenType,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parameter {
|
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 {
|
Self {
|
||||||
name: Token {
|
name: Token {
|
||||||
token_type: TokenType::StringType,
|
token_type: TokenType::StringType,
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::ast_pass::Expression::NamedParameter;
|
use crate::compiler::ast_pass::Expression::NamedParameter;
|
||||||
use crate::ast_pass::{Expression, Function, Parameter, Statement};
|
use crate::compiler::ast_pass::{Expression, Function, Parameter, Statement};
|
||||||
use crate::builtins::lookup;
|
use crate::builtins::lookup;
|
||||||
use crate::chunk::Chunk;
|
use crate::chunk::Chunk;
|
||||||
use crate::errors::CompilerError::{IncompatibleTypes, UndeclaredVariable};
|
use crate::errors::CompilerError::{IncompatibleTypes, UndeclaredVariable};
|
||||||
|
|
@ -22,7 +22,7 @@ pub fn compile(
|
||||||
compile_in_namespace(ast, qualified_name, symbols, registry)
|
compile_in_namespace(ast, qualified_name, symbols, registry)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn compile_function(
|
pub fn compile_function(
|
||||||
function: &Function,
|
function: &Function,
|
||||||
symbols: &SymbolTable,
|
symbols: &SymbolTable,
|
||||||
registry: &mut Registry,
|
registry: &mut Registry,
|
||||||
|
|
@ -41,7 +41,7 @@ pub(crate) fn compile_function(
|
||||||
Ok(chunk)
|
Ok(chunk)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn compile_in_namespace(
|
pub fn compile_in_namespace(
|
||||||
ast: &Vec<Statement>,
|
ast: &Vec<Statement>,
|
||||||
namespace: Option<&str>,
|
namespace: Option<&str>,
|
||||||
symbols: &SymbolTable,
|
symbols: &SymbolTable,
|
||||||
|
|
@ -54,7 +54,7 @@ pub(crate) fn compile_in_namespace(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Compiler {
|
pub struct Compiler {
|
||||||
chunk: Chunk,
|
chunk: Chunk,
|
||||||
_had_error: bool,
|
_had_error: bool,
|
||||||
current_line: usize,
|
current_line: usize,
|
||||||
|
|
@ -62,7 +62,7 @@ pub(crate) struct Compiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Compiler {
|
impl Compiler {
|
||||||
pub(crate) fn new(name: &str) -> Self {
|
pub fn new(name: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
chunk: Chunk::new(name),
|
chunk: Chunk::new(name),
|
||||||
_had_error: false,
|
_had_error: false,
|
||||||
|
|
@ -72,7 +72,7 @@ impl Compiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// compile the entire AST into a chunk, adding a RETURN OP
|
/// compile the entire AST into a chunk, adding a RETURN OP
|
||||||
pub(crate) fn compile(
|
pub fn compile(
|
||||||
&mut self,
|
&mut self,
|
||||||
ast: &Vec<Statement>,
|
ast: &Vec<Statement>,
|
||||||
symbols: &SymbolTable,
|
symbols: &SymbolTable,
|
||||||
4
src/compiler/mod.rs
Normal file
4
src/compiler/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
mod compiler_tests;
|
||||||
|
pub mod bytecode_pass;
|
||||||
|
pub mod scan_pass;
|
||||||
|
pub mod ast_pass;
|
||||||
39
src/lib.rs
39
src/lib.rs
|
|
@ -1,24 +1,20 @@
|
||||||
use crate::ast_pass::{Expression, Statement};
|
|
||||||
use crate::chunk::Chunk;
|
use crate::chunk::Chunk;
|
||||||
|
use crate::compiler::ast_pass::{Expression, Statement};
|
||||||
use crate::errors::CrudLangError::Platform;
|
use crate::errors::CrudLangError::Platform;
|
||||||
use crate::errors::{CompilerErrorAtLine, CrudLangError};
|
use crate::errors::{CompilerErrorAtLine, CrudLangError};
|
||||||
use crate::scan_pass::scan;
|
|
||||||
use crate::symbol_builder::Symbol;
|
use crate::symbol_builder::Symbol;
|
||||||
|
use crate::value::Value::Void;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
use crate::value::Value::Void;
|
|
||||||
|
|
||||||
pub mod ast_pass;
|
|
||||||
mod builtins;
|
mod builtins;
|
||||||
pub mod bytecode_pass;
|
|
||||||
pub mod chunk;
|
pub mod chunk;
|
||||||
mod compiler_tests;
|
pub(crate) mod compiler;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod file_watch;
|
pub mod file_watch;
|
||||||
mod keywords;
|
mod keywords;
|
||||||
pub mod repl;
|
pub mod repl;
|
||||||
pub mod scan_pass;
|
|
||||||
mod symbol_builder;
|
mod symbol_builder;
|
||||||
mod tokens;
|
mod tokens;
|
||||||
mod value;
|
mod value;
|
||||||
|
|
@ -37,19 +33,14 @@ pub fn compile_sourcedir(source_dir: &str) -> Result<HashMap<String, Chunk>, Cru
|
||||||
if path.ends_with(".crud") {
|
if path.ends_with(".crud") {
|
||||||
print!("-- Compiling {} -- ", path);
|
print!("-- Compiling {} -- ", path);
|
||||||
let source = fs::read_to_string(path).map_err(map_underlying())?;
|
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();
|
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) => {
|
Ok(statements) => {
|
||||||
let path = path.strip_prefix(source_dir).unwrap().replace(".crud", "");
|
let path = path.strip_prefix(source_dir).unwrap().replace(".crud", "");
|
||||||
|
|
||||||
symbol_builder::build(&path, &statements, &mut symbol_table);
|
symbol_builder::build(&path, &statements, &mut symbol_table);
|
||||||
bytecode_pass::compile(
|
compiler::bytecode_pass::compile(Some(&path), &statements, &symbol_table, &mut registry)?;
|
||||||
Some(&path),
|
|
||||||
&statements,
|
|
||||||
&symbol_table,
|
|
||||||
&mut registry,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("{}", e);
|
println!("{}", e);
|
||||||
|
|
@ -67,32 +58,32 @@ pub fn map_underlying() -> fn(std::io::Error) -> CrudLangError {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recompile(src: &str, registry: &mut HashMap<String, Chunk>) -> Result<(), 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 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);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile(src: &str) -> Result<HashMap<String, Chunk>, CrudLangError> {
|
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 registry = HashMap::new();
|
||||||
let mut symbol_table = 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);
|
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)
|
Ok(registry)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub(crate) fn run(src: &str) -> Result<value::Value, CrudLangError> {
|
pub(crate) fn run(src: &str) -> Result<value::Value, CrudLangError> {
|
||||||
let tokens = scan(src)?;
|
let tokens = compiler::scan_pass::scan(src)?;
|
||||||
let mut symbol_table = 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);
|
symbol_builder::build("", &ast, &mut symbol_table);
|
||||||
let mut registry = HashMap::new();
|
let mut registry = HashMap::new();
|
||||||
bytecode_pass::compile(None, &ast, &symbol_table, &mut registry)?;
|
compiler::bytecode_pass::compile(None, &ast, &symbol_table, &mut registry)?;
|
||||||
|
|
||||||
let registry = arc_swap::ArcSwap::from(std::sync::Arc::new(registry));
|
let registry = arc_swap::ArcSwap::from(std::sync::Arc::new(registry));
|
||||||
vm::interpret(registry.load(), "main").map_err(CrudLangError::from)
|
vm::interpret(registry.load(), "main").map_err(CrudLangError::from)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::chunk::Chunk;
|
use crate::chunk::Chunk;
|
||||||
use crate::errors::CrudLangError;
|
use crate::errors::CrudLangError;
|
||||||
use crate::scan_pass::scan;
|
use crate::compiler::scan_pass::scan;
|
||||||
use crate::vm::Vm;
|
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 arc_swap::ArcSwap;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
|
||||||
|
|
@ -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::builtins::lookup;
|
||||||
use crate::errors::CompilerError;
|
use crate::errors::CompilerError;
|
||||||
use crate::errors::CompilerError::IncompatibleTypes;
|
use crate::errors::CompilerError::IncompatibleTypes;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue