1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

refactor: merge all repos

This commit is contained in:
Natapat Samutpong 2022-01-26 15:07:53 +07:00
parent 4aba0cf621
commit 09e8a97d34
18 changed files with 152 additions and 359 deletions

13
Cargo.lock generated
View file

@ -41,19 +41,10 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
name = "blspc"
version = "0.1.0"
dependencies = [
"middle",
"regex",
"structopt",
]
[[package]]
name = "blvm"
version = "0.1.0"
dependencies = [
"middle",
"structopt",
]
[[package]]
name = "clap"
version = "2.34.0"
@ -105,10 +96,6 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "middle"
version = "0.1.0"
[[package]]
name = "proc-macro-error"
version = "1.0.4"

View file

@ -1,6 +1,4 @@
[workspace]
members = [
"blspc",
"blvm",
"middle",
]

View file

@ -1,11 +1,12 @@
all: build-blspc build-blvm
all: build
debug: build-debug
build-blspc:
build:
cd ./blspc; cargo build --release
rm ~/bin/blspc -f
mv ./target/release/blspc ~/bin/blspc
build-debug:
cd ./blspc; cargo build
rm ~/bin/blspc -f
mv ./target/debug/blspc ~/bin/blspc
build-blvm:
cd ./blvm; cargo build
rm ~/bin/blvm -f
mv ./target/debug/blvm ~/bin/blvm

View file

@ -12,6 +12,5 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
middle = { path = "../middle" }
regex = "1.5.4"
structopt = "0.3.26"

View file

@ -9,7 +9,19 @@ pub struct Args {
#[structopt(short, long, parse(from_occurrences))]
pub verbose: u8,
/// Compliation mode (-c).
#[structopt(short, long)]
pub compile: bool,
/// Run mode (-r).
#[structopt(short, long)]
pub run: bool,
/// Files to process.
#[structopt(name = "FILE", parse(from_os_str))]
pub file: PathBuf,
/// Output file.
#[structopt(short, long, parse(from_os_str))]
pub output: Option<PathBuf>,
}

View file

@ -1,7 +1,4 @@
use crate::parser::Sexpr::{self, *};
use middle::*;
use crate::{compiler::instr::*, parser::Sexpr::{self, *}};
pub struct Compiler {
pub instructions: Vec<Instr>,
pub register_pointer: usize,

View file

@ -1,4 +1,4 @@
use std::fmt::Display;
use std::{fmt::Display, str::FromStr};
/// Literal types for the assembler.
#[derive(Clone, Debug)]
@ -20,6 +20,35 @@ impl Display for Type {
}
}
impl FromStr for Type {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with("$") {
return Err(format!("Invalid literal: {}", s));
}
let s = &s[1..];
match s {
"true" => Ok(Type::Boolean(true)),
"false" => Ok(Type::Boolean(false)),
_ => {
let fl = s.parse::<f64>();
if fl.is_ok() {
Ok(Type::Float(fl.unwrap()))
} else {
let i = s.parse::<i64>();
if i.is_ok() {
Ok(Type::Int(i.unwrap()))
} else {
Ok(Type::String(s.to_string()))
}
}
}
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Register { pub value: usize }
@ -29,6 +58,15 @@ impl Display for Register {
}
}
impl FromStr for Register {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = s[1..].parse::<usize>().map_err(|_| ())?;
Ok(Register { value })
}
}
/// Instructions for the assembler.
#[derive(Clone, Debug)]
pub enum Instr {

View file

@ -1 +1,4 @@
/// Definition of assembler's instructions.
pub mod instr;
/// Definition of the compiler.
pub mod compile;

View file

@ -1,4 +1,4 @@
use std::{fs::{read_to_string, File}, path::Path, io::Write, time::Instant};
use std::{fs::{read_to_string, File}, path::Path, io::{Write, Seek}, time::Instant};
use structopt::StructOpt;
@ -9,66 +9,54 @@ mod util;
use util::cover_paren;
mod parser;
use parser::{tokenize, Parser};
use parser::{tokenize, Parser, Sexpr};
mod compiler;
use compiler::compile::Compiler;
use compiler::{compile::Compiler, instr::Instr};
mod vm;
fn main() {
let start = Instant::now();
let args = Args::from_args();
let src = cover_paren(read_to_string(&args.file).unwrap());
let file_name = Path::new(&args.file).file_stem().unwrap().to_str().unwrap();
let file_name = match args.output {
Some(path) => path,
None => Path::new(&args.file).to_path_buf(),
}.file_stem().unwrap().to_str().unwrap().to_string();
let tokens = tokenize(&src);
let mut parser = Parser::new(tokens.clone());
let result = parser.parse();
match args.verbose {
0 => {
let mut file = File::create(format!("{}.bbb", file_name)).unwrap();
let mut compiler = Compiler::new();
let before = Instant::now();
for instr in compiler.compile(result.unwrap(), 0).unwrap() {
write!(file, "{}\n", instr).unwrap();
}
let spent = before.elapsed();
let total = start.elapsed();
println!("Compiled in {}.{}s, Total of {}.{}s", spent.as_secs(), spent.subsec_millis(), total.as_secs(), total.subsec_millis());
match result {
Ok(ast) => {
compile(ast, file_name, start);
},
1 => {
println!("Parsed AST: {:#?}", result);
Err(e) => {
eprintln!("{}", e);
}
}
let mut file = File::create(format!("{}.bbb", file_name)).unwrap();
}
let mut compiler = Compiler::new();
let before = Instant::now();
for instr in compiler.compile(result.unwrap(), 0).unwrap() {
write!(file, "{}\n", instr).unwrap();
fn compile(ast: Sexpr, file_name: String, start: Instant) {
let mut compiler = Compiler::new();
let code = compiler.compile(ast, 0);
match code {
Ok(code) => {
let mut file = File::create(format!("{}.bsm", file_name)).unwrap();
for line in code {
write!(file, "{}\n", line).unwrap();
}
let spent = before.elapsed();
let total = start.elapsed();
file.seek(std::io::SeekFrom::End(-1)).unwrap(); // Trim last newline
println!("Compiled in {}.{}s, Total of {}.{}s", spent.as_secs(), spent.subsec_millis(), total.as_secs(), total.subsec_millis());
let elapsed = start.elapsed();
println!("Compiled in {}.{}s", elapsed.as_secs(), elapsed.subsec_millis());
},
2 | _ => {
println!("Tokens: {:?}", tokens);
println!("Parsed AST: {:#?}", result);
let mut file = File::create(format!("{}.bbb", file_name)).unwrap();
let mut compiler = Compiler::new();
let before = Instant::now();
for instr in compiler.compile(result.unwrap(), 0).unwrap() {
write!(file, "{}\n", instr).unwrap();
}
let spent = before.elapsed();
let total = start.elapsed();
println!("Compiled in {}.{}s, Total of {}.{}s", spent.as_secs(), spent.subsec_millis(), total.as_secs(), total.subsec_millis());
Err(err) => {
eprintln!("{}", err);
}
}
}

1
blspc/src/vm/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod parse;

58
blspc/src/vm/parse.rs Normal file
View file

@ -0,0 +1,58 @@
use crate::compiler::instr::*;
pub fn parse(src: &str) -> Vec<Instr> {
let mut result = Vec::new();
for line in src.lines() {
// <label>: <instr> <arg>+
let mut parts = line.split_whitespace();
let label = parts.next();
if label == Some(";") { continue; }
let instr = parts.next();
let mut args = Vec::new();
let mut in_quote = false;
let mut str = String::new();
while let Some(part) = parts.next() { match in_quote {
true => {
if part.ends_with("\"") {
str.push_str(&format!(" {}", part));
args.push(str);
str = String::new();
in_quote = false;
} else { str.push_str(&format!(" {}", part)); }
},
false => {
if part.starts_with("$\"") {
str.push_str(&part);
in_quote = true;
} else { args.push(part.to_string()); }
}
}
}
result.push(match instr {
Some("STORE") => {
let address = args[0].parse::<Register>().unwrap();
let value = args[1].parse::<Type>().unwrap();
let label = label.map(|l| l[..1].parse::<usize>().unwrap()).unwrap();
Instr::Store { address, value, label }
},
Some("CALL") => {
let address = args[0].parse::<Register>().unwrap();
let args = args[1].parse::<Register>().unwrap();
let label = label.map(|l| l[..1].parse::<usize>().unwrap()).unwrap();
Instr::Call { address, args, label }
},
Some("RETURN") => {
let value = args[0].parse::<Register>().unwrap();
let label = label.map(|l| l[..1].parse::<usize>().unwrap()).unwrap();
Instr::Return { value, label }
},
_ => panic!("Unknown instruction: {}", instr.unwrap())
});
}
result
}

1
blvm/.gitignore vendored
View file

@ -1 +0,0 @@
/target

225
blvm/Cargo.lock generated
View file

@ -1,225 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[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 = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "blvm"
version = "0.1.0"
dependencies = [
"structopt",
]
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0005d08a8f7b65fb8073cb697aa0b12b631ed251ce73d862ce50eeb52ce3b50"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-segmentation"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[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

@ -1,10 +0,0 @@
[package]
name = "blvm"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
middle = { path = "../middle" }
structopt = "0.3.26"

View file

@ -1,11 +0,0 @@
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "blvm")]
pub struct Args {
/// Files to process.
#[structopt(name = "FILE", parse(from_os_str))]
pub file: PathBuf,
}

View file

@ -1,17 +0,0 @@
use std::{fs::read_to_string, path::Path};
use structopt::StructOpt;
mod args;
use args::Args;
mod parser;
use parser::tokenize;
fn main() {
let args = Args::from_args();
let src = read_to_string(&args.file).unwrap();
let _file_name = Path::new(&args.file).file_stem().unwrap().to_str().unwrap();
let tokens = tokenize(&src);
}

View file

@ -1,17 +0,0 @@
use middle::*;
pub fn tokenize(src: &str) -> Vec<Instr> {
let mut result = Vec::new();
for line in src.lines() {
// <label>: <instr> <arg>+
let mut parts = line.split_whitespace();
let label = parts.next();
let instr = parts.next();
let args = parts.collect::<Vec<_>>();
println!("{:?} {:?} {:?}", label, instr, args);
}
result
}

View file

@ -1,8 +0,0 @@
[package]
name = "middle"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]