Skip to content

Commit

Permalink
Merge pull request #58 from zom-lang/parser-remake
Browse files Browse the repository at this point in the history
Remake of the parser
  • Loading branch information
thi8v authored Apr 6, 2024
2 parents 31cdaa1 + dce0be1 commit a4120a2
Show file tree
Hide file tree
Showing 36 changed files with 2,709 additions and 3,247 deletions.
349 changes: 20 additions & 329 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ readme = "README.md"
[workspace.dependencies]
inkwell = { version = "0.2.0", default-features = false }
criterion = "0.5.0"
termcolor = "1.4.1"
lazy_static = "1.4.0"
zom_lexer = { path = "stage1/zom_lexer" }
zom_parser = { path = "stage1/zom_parser" }
zom_common = { path = "stage1/zom_common" }
zom_compiler = { path = "stage1/zom_compiler" }
zom_codegen = { path = "stage1/zom_codegen" }
zom_errors = { path = "stage1/zom_errors" }
30 changes: 21 additions & 9 deletions example/test.zom
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
pub const test2 = 123 * 12 + 1;
package hello.main

fn fourteen(ptr: *const u8) u8 {
const test1 = undefined;
const test2 = 't';
var hello_world = "Hello world!".len;
return a.*(12, 144);
import std.fmt.println // <- NEW better import item! ;)
import std.fmt as f

fn add(a: u32,b: u32) u32 {
var demo: u32
f.println("Hello world!")
a, b = 123, 456
return a + b
}

extern "c" fn c_function() void;
const test: u32 = 2

fn max(a: u32, b: u32) u32 {
if (a > b)
return a
else
return b
// ^ a conditional stmt
}

extern "c" fn c_function2() void {
// or with only one return stmt

fn maxSimple(a: u32, b: u32) u32 {
return a if a > b else b
// ^ a conditional expression
}

3 changes: 2 additions & 1 deletion stage1/zom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ zom_parser.workspace = true
zom_common.workspace = true
zom_codegen.workspace = true
zom_compiler.workspace = true

zom_errors.workspace = true
termcolor.workspace = true

[features]
default = ["llvm15-0"]
Expand Down
2 changes: 1 addition & 1 deletion stage1/zom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ macro_rules! err {
Err(err!(raw $msg))
);

(fmt $msg:tt, $($arg:expr)*) => ({
(fmt $msg:tt $(, $arg:expr)*) => ({
use $crate::SError;
Err(Box::new(SError::new(format!($msg, $( $arg ),* ))))
});
Expand Down
31 changes: 1 addition & 30 deletions stage1/zom/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,8 @@
use std::backtrace::BacktraceStatus::*;
use std::panic;
use std::thread;
use std::{backtrace::Backtrace, error::Error};
use std::error::Error;

use zom::{run_with_args, ExitStatus};
use zom_common::error::ZomError;

fn main() -> Result<(), Box<dyn Error>> {
panic::set_hook(Box::new(|panic_info| {
let thread = thread::current();
let backtrace = Backtrace::capture();

if let Some(name) = thread.name() {
println!("thread '{}' {}", name, panic_info)
} else {
println!("{}", panic_info);
}

match backtrace.status() {
Captured => {
println!("{}", backtrace);
}
Disabled => println!(
"note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
),
Unsupported => println!("note: backtrace is not supported."),
_ => {}
}

let ice = ZomError::ice_error(panic_info.to_string());
println!("\n{}", ice);
}));

let status = match run_with_args(std::env::args_os()) {
Ok(v) => v,
Err(err) => {
Expand Down
119 changes: 3 additions & 116 deletions stage1/zom/src/ops/bobj.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
use std::{
error::Error,
fs,
// mem,
path::PathBuf,
};

use inkwell::{context::Context, passes::PassManager};
// use zom_codegen::gen::CodeGen;
// use zom_compiler::compiler::Compiler;
use zom_parser::{parse, ParserSettings, ParsingContext};

use zom_lexer::Lexer;

use crate::{err, ExitStatus};
use crate::ExitStatus;

#[derive(clap::Args, Debug, Clone)]
pub struct Args {
Expand All @@ -37,111 +29,6 @@ pub struct Args {
verbose: bool,
}

pub fn build(mut args: Args) -> Result<ExitStatus, Box<dyn Error>> {
// default ouput_file to `output.o`, it's where because with `default_value_t`, that doesn't work.
if args.output_file.is_none() {
args.output_file = if args.emit_ir {
Some(PathBuf::from(r"output.ll"))
} else {
Some(PathBuf::from(r"output.o"))
};
}

let source = match fs::read_to_string(&mut args.source_file) {
Ok(src) => src,
Err(_) => return err!("Error while trying to read the source file."),
};

let mut lexer = Lexer::new(source.as_str(), &args.source_file);

let tokens = match lexer.lex() {
Ok(src) => src,
Err(err) => return err!(fmt "\n{:?}\n", err),
};

args.verbose.then(|| {
println!("[+] Successfully lexes the input.");
});

let parse_context = ParsingContext::new(args.source_file.to_str().unwrap().to_owned(), source);

let parse_result = parse(
tokens.as_slice(),
&[],
&mut ParserSettings::default(),
parse_context,
);

let _ast;

match parse_result {
Ok((parsed_ast, rest)) => {
if rest.is_empty() {
_ast = parsed_ast;
} else {
return err!("There is rest after parsing.");
}
}
Err(err) => return err!(fmt "\n{:?}\n", err),
}

args.verbose.then(|| {
println!("[+] Successfully parsed the tokens.");
});

let context = Context::create();
let module = context.create_module(args.source_file.to_str().unwrap());
let _builder = context.create_builder();

// Create FPM
let fpm = PassManager::create(&module);

fpm.add_instruction_combining_pass();
fpm.add_reassociate_pass();
fpm.add_gvn_pass();
fpm.add_cfg_simplification_pass();
fpm.add_basic_alias_analysis_pass();
fpm.add_promote_memory_to_register_pass();
fpm.add_instruction_combining_pass();
fpm.add_reassociate_pass();

fpm.initialize();

todo!()
// let module = context.create_module(mem::take(
// &mut args.source_file.as_mut_os_str().to_str().unwrap(),
// ));

// let gen_res = CodeGen::compile_ast(&context, &builder, &fpm, &module, &ast);

// match gen_res {
// Ok(_) => {
// args.verbose.then(|| {
// println!("[+] Successfully generate the code.");
// });
// if args.emit_ir {
// match module.print_to_file(args.output_file.clone().unwrap().as_path()) {
// Ok(_) => {}
// Err(err) => return Err(anyhow!(format!("{}", err))),
// }
// args.verbose.then(|| {
// println!("Wrote the result to {:?}!", args.output_file.unwrap());
// });
// return Ok(ExitStatus::Success);
// }
// match args.output_file {
// Some(ref path) => {
// Compiler::compile_default(module, path)
// .expect("Couldn't compile to object file");
// args.verbose.then(|| {
// println!("[+] Successfully compile the code.");
// });
// println!("Wrote result to {:?}!", path);
// Ok(ExitStatus::Success)
// }
// None => Err(anyhow!("Couldn't unwrap the file path")),
// }
// }
// Err(err) => Err(anyhow!(format!("{}", err))),
// }
pub fn build(_args: Args) -> Result<ExitStatus, Box<dyn Error>> {
todo!("Will be removed later with #42")
}
53 changes: 22 additions & 31 deletions stage1/zom/src/ops/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use std::error::Error;
use std::fs;
use std::io::{self, stdout, Write};
use std::path::PathBuf;
use termcolor::ColorChoice;
use zom_lexer::Lexer;
use zom_parser::{parse, ParserSettings, ParsingContext};
use zom_parser::*;

use zom_errors::prelude::*;

use crate::{err, ExitStatus};

Expand Down Expand Up @@ -32,16 +35,15 @@ pub fn dev() -> Result<ExitStatus, Box<dyn Error>> {
println!("file path = {}", path.display());
println!("buffer = \\\n{}\n\\-> {}\n", buffer, buffer.len());

let mut lexer = Lexer::new(&buffer, &path);
let lctx = LogContext::new(&buffer, &path, ColorChoice::Always);

let mut lexer = Lexer::new(&buffer, &path, lctx);

let tokens = match lexer.lex() {
Ok(t) => t,
Err(errs) => {
let mut err = "".to_owned();
for error in errs {
err += format!("{}\n", error).as_str();
}
return err!(fmt "{}", err);
let (tokens, lctx) = match lexer.lex() {
FinalRes::Ok(t, lctx) => (t, lctx),
FinalRes::Err(logs) => {
logs.print();
return err!("");
}
};

Expand All @@ -51,29 +53,18 @@ pub fn dev() -> Result<ExitStatus, Box<dyn Error>> {
}

println!("\n~~~ SEPARTOR ~~~");
let parser = Parser::new(&tokens, lctx);

let parse_context = ParsingContext::new("<dev_cmd>.zom".to_owned(), buffer);

let ast_result = parse(
tokens.as_slice(),
&[],
&mut ParserSettings::default(),
parse_context,
);

match ast_result {
Ok((ast, rest_toks)) => {
println!("ast = {:#?}", ast);
println!("toks_rest = {:?}", rest_toks);
let (ast, lctx) = match parser.parse() {
FinalRes::Ok(ast, lctx) => (ast, lctx),
FinalRes::Err(logs) => {
logs.print();
return err!("");
}
Err(errs) => {
let mut err = "".to_owned();
for error in errs {
err += format!("{}\n", error).as_str();
}
return err!(fmt "{}", err);
}
}
};

dbg!(ast);

lctx.print();
Ok(ExitStatus::Success)
}
Loading

0 comments on commit a4120a2

Please sign in to comment.