pull/4/head
azur 2022-05-04 10:49:15 +07:00
parent d8fbc17a33
commit 9c3a390ae8
6 changed files with 111 additions and 8 deletions

54
Cargo.lock generated
View File

@ -11,6 +11,17 @@ dependencies = [
"const-random",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -33,6 +44,17 @@ dependencies = [
"syntax",
]
[[package]]
name = "colored"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]]
name = "const-random"
version = "0.1.13"
@ -72,11 +94,21 @@ dependencies = [
"wasi",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "hzc"
version = "0.1.0"
dependencies = [
"codegen",
"colored",
"syntax",
]
@ -126,3 +158,25 @@ name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -5,13 +5,15 @@ pub struct Codegen {
/// The emitted code.
/// When the codegen is done, this will be joined into a single string
pub emitted: Vec<String>,
/// Finalized code in bytes
pub finalized: Vec<u8>,
}
impl Default for Codegen { fn default() -> Self { Self::new() } }
impl Codegen {
pub fn new() -> Codegen {
Codegen { emitted: Vec::new() }
Codegen { emitted: Vec::new(), finalized: Vec::new() }
}
/// Emit a string to the output.
@ -183,4 +185,9 @@ impl Codegen {
.map(|ty| self.gen_typehint(&ty.0)).collect::<Vec<_>>().join(" | "),
}
}
/// Finalize the code generation.
pub fn finalize(&mut self) {
self.finalized = self.emitted.join("\n").as_bytes().to_vec();
}
}

View File

@ -5,4 +5,6 @@ edition = "2021"
[dependencies]
syntax = { path = "../syntax" }
codegen = { path = "../codegen" }
codegen = { path = "../codegen" }
colored = "2.0.0"

View File

@ -3,6 +3,8 @@ use std::{fs::File, io::Write};
use syntax::{lex::lex, parse::parse};
use codegen::Codegen;
pub mod util;
fn main() {
let path = std::env::args().nth(1).expect("No file specified");
let input = std::fs::read_to_string(path).expect("Failed to read file");
@ -29,12 +31,17 @@ fn main() {
return;
}
info!("Parsed in {}ms", time.elapsed().as_millis());
//
// Codegen
//
let mut codegen = Codegen::new();
codegen.gen(ast.unwrap());
codegen.finalize();
let mut file = File::create("out.ts").unwrap();
file.write_all(codegen.emitted.join("\n").as_bytes()).unwrap();
file.write_all(&codegen.finalized).unwrap();
info!("Generated {} bytes in {} ms", codegen.finalized.len(), time.elapsed().as_millis());
}

38
core/src/util.rs Normal file
View File

@ -0,0 +1,38 @@
use colored::Colorize;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LogLevel { Debug, Info, Warn, Error }
fn prefix (level: LogLevel) -> String {
match level {
LogLevel::Debug => "DEBG ".bright_black(),
LogLevel::Info => "INFO ".blue(),
LogLevel::Warn => "WARN ".yellow(),
LogLevel::Error => "ERRO ".red(),
}.to_string()
}
pub fn log <S: Into<String>>(level: LogLevel, message: S) {
println!("{}{}", prefix(level), message.into());
}
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {
$crate::util::log( $crate::util::LogLevel::Info, format!($($arg)*) );
};
}
#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {
$crate::util::log( $crate::util::LogLevel::Warn, format!($($arg)*) );
};
}
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {
$crate::util::log( $crate::util::LogLevel::Error, format!($($arg)*) );
};
}

View File

@ -99,11 +99,6 @@ pub enum Expr {
t: Box<Spanned<Self>>,
f: Box<Spanned<Self>>
},
Case {
cond: Box<Spanned<Self>>,
cases: Spanned<Vec<(Spanned<Self>, Spanned<Self>)>>,
default: Box<Spanned<Self>>
},
Do {
body: Spanned<Vec<Spanned<Self>>>
},