NOT WORKING initial

This commit is contained in:
MCorange 2023-03-12 22:51:21 +02:00
commit cfcea89dac
19 changed files with 1029 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

530
Cargo.lock generated Normal file
View File

@ -0,0 +1,530 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "backtrace"
version = "0.3.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "233d376d6d185f2a3093e58f283f60f880315b6c60075b01f36b3b85154564ca"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d7ae14b20b94cb02149ed21a86c423859cbe18dc7ed69845cace50e52b40a5"
dependencies = [
"bitflags",
"clap_derive",
"clap_lex",
"is-terminal",
"once_cell",
"strsim",
"termcolor",
]
[[package]]
name = "clap_derive"
version = "4.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44bec8e5c9d09e439c4335b1af0abaab56dcf3b94999a936e1bb47b9134288f0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350b9cf31731f9957399229e9b2adc51eeabdfbe9d71d9a0552275fd12710d09"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "color-eyre"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "eyre"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "gimli"
version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "io-lifetimes"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfa919a82ea574332e2de6e74b4c36e74d41982b335080fa59d4ef31be20fdf3"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "is-terminal"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b6b32576413a8e69b90e952e4a026476040d81017b80445deda5f2d3921857"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys",
]
[[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.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "linux-raw-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
[[package]]
name = "mclang"
version = "0.1.0"
dependencies = [
"clap",
"color-eyre",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "miniz_oxide"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa"
dependencies = [
"adler",
]
[[package]]
name = "object"
version = "0.30.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea86265d3d3dcb6a27fc51bd29a4bf387fae9d2986b823079d4986af253eb439"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "os_str_bytes"
version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]]
name = "pin-project-lite"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
[[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.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]]
name = "rustix"
version = "0.36.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd5c6ff11fecd55b40746d1995a02f2eb375bf8c00d192d521ee09f42bef37bc"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "sharded-slab"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [
"lazy_static",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]]
name = "thread_local"
version = "1.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]]
name = "tracing"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-error"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
]
[[package]]
name = "unicode-ident"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[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-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
[[package]]
name = "windows_i686_gnu"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
[[package]]
name = "windows_i686_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "mclang"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.1.8", features = ["derive"] }
color-eyre = "0.6.2"

BIN
a Executable file

Binary file not shown.

52
a.nasm Normal file
View File

@ -0,0 +1,52 @@
global _start
segment .text
print:
mov r8, -3689348814741910323
sub rsp, 40
mov BYTE [rsp+32], 10
lea rcx, [rsp+31]
.L2:
mov rax, rdi
mul r8
mov rax, rdi
shr rdx, 3
lea rsi, [rdx+rdx*4]
add rsi, rsi
sub rax, rsi
add eax, 48
mov BYTE [rcx], al
mov rax, rdi
mov rdi, rdx
mov rdx, rcx
sub rcx, 1
cmp rax, 9
ja .L2
lea rcx, [rsp+32]
lea rax, [rsp+32]
mov edi, 1
sub rax, rdx
sub rdx, rcx
lea rsi, [rsp+32+rdx]
mov rdx, rax
mov rax, 1
syscall
add rsp, 40
ret
_start:
; -- PUSH 35
mov rax, 35
push rax
; -- PUSH 34
mov rax, 34
push rax
; -- PLUS
pop rax
pop rbx
add rax, rbx
push rax
; -- PRINT
pop rdi
call print
mov rax, 60
mov rdi, 0
syscall

BIN
a.o Normal file

Binary file not shown.

54
src/compile/commands.rs Normal file
View File

@ -0,0 +1,54 @@
use std::path::PathBuf;
use std::process::{Command, Stdio};
use color_eyre::Result;
use crate::compile::logger;
pub fn linux_x86_64_compile_and_link(of_a: PathBuf, of_o: PathBuf, of_c: PathBuf) -> Result<()> {
let nasm_args = [
"-felf64",
of_a.to_str().unwrap(),
"-o",
of_o.to_str().unwrap()
];
let ld_args = [
of_o.to_str().unwrap(),
"-o",
of_c.to_str().unwrap()
];
let mut proc = if cfg!(target_os = "windows") {
todo!("Windows compiling");
} else {
Command::new("nasm")
.args(&nasm_args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?
};
logger::info(format!("running 'nasm {}'", nasm_args.join(" ")).as_str());
let exit = proc.wait()?;
logger::info(format!("nasm process exited with code {}", exit).as_str());
let mut proc2 = if cfg!(target_os = "windows") {
todo!("Windows compiling");
} else {
Command::new("ld")
.args(&ld_args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?
};
logger::info(format!("running 'ld {}'", ld_args.join(" ")).as_str());
let exit2 = proc2.wait()?;
logger::info(format!("ld process exited with code {}", exit2).as_str());
Ok(())
}

View File

@ -0,0 +1,98 @@
use std::{fs, path::PathBuf, io::{Write, BufWriter}};
use crate::{constants::{Operator, OpType}, Args};
use color_eyre::Result;
use crate::compile::commands;
use super::commands::linux_x86_64_compile_and_link;
pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
let mut of_c = PathBuf::from(&args.out_file);
let mut of_o = PathBuf::from(&args.out_file);
let mut of_a = PathBuf::from(&args.out_file);
of_c.set_extension("");
of_o.set_extension("o");
of_a.set_extension("nasm");
let file = fs::File::create(&of_a)?;
let mut writer = BufWriter::new(&file);
writeln!(writer, "global _start")?;
writeln!(writer, "segment .text")?;
writeln!(writer, " print:")?;
writeln!(writer, " mov r8, -3689348814741910323")?;
writeln!(writer, " sub rsp, 40")?;
writeln!(writer, " mov BYTE [rsp+32], 10")?;
writeln!(writer, " lea rcx, [rsp+31]")?;
writeln!(writer, ".L2:")?;
writeln!(writer, " mov rax, rdi")?;
writeln!(writer, " mul r8")?;
writeln!(writer, " mov rax, rdi")?;
writeln!(writer, " shr rdx, 3")?;
writeln!(writer, " lea rsi, [rdx+rdx*4]")?;
writeln!(writer, " add rsi, rsi")?;
writeln!(writer, " sub rax, rsi")?;
writeln!(writer, " add eax, 48")?;
writeln!(writer, " mov BYTE [rcx], al")?;
writeln!(writer, " mov rax, rdi")?;
writeln!(writer, " mov rdi, rdx")?;
writeln!(writer, " mov rdx, rcx")?;
writeln!(writer, " sub rcx, 1")?;
writeln!(writer, " cmp rax, 9")?;
writeln!(writer, " ja .L2")?;
writeln!(writer, " lea rcx, [rsp+32]")?;
writeln!(writer, " lea rax, [rsp+32]")?;
writeln!(writer, " mov edi, 1")?;
writeln!(writer, " sub rax, rdx")?;
writeln!(writer, " sub rdx, rcx")?;
writeln!(writer, " lea rsi, [rsp+32+rdx]")?;
writeln!(writer, " mov rdx, rax")?;
writeln!(writer, " mov rax, 1")?;
writeln!(writer, " syscall")?;
writeln!(writer, " add rsp, 40")?;
writeln!(writer, " ret")?;
writeln!(writer, "_start:")?;
for token in tokens {
match token.typ {
OpType::Push => {
writeln!(writer, " ; -- PUSH {}", token.value)?;
writeln!(writer, " mov rax, {}", token.value)?;
writeln!(writer, " push rax")?;
},
OpType::Pop => {
writeln!(writer, " ; -- POP")?;
writeln!(writer, " pop")?;
},
OpType::Plus => {
writeln!(writer, " ; -- PLUS")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rbx")?;
writeln!(writer, " add rax, rbx")?;
writeln!(writer, " push rax")?;
},
OpType::Minus => {
writeln!(writer, " ; -- MINUS")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rbx")?;
writeln!(writer, " sub rbx, rax")?;
writeln!(writer, " push rax")?;
},
OpType::Print => {
writeln!(writer, " ; -- PRINT")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " call print")?;
},
}
}
writeln!(writer, " mov rax, 60")?;
writeln!(writer, " mov rdi, 0")?;
writeln!(writer, " syscall")?;
writer.flush()?;
linux_x86_64_compile_and_link(of_a, of_o, of_c)?;
Ok(())
}

9
src/compile/logger.rs Normal file
View File

@ -0,0 +1,9 @@
use crate::util::color;
pub fn error(msg: &str) {
eprintln!("{red}error{r}: {msg}", red=color::FG_RED, r=color::RESET);
}
pub fn info(msg: &str) {
println!("{green}info{r}: {msg}", green=color::FG_GREEN, r=color::RESET);
}

4
src/compile/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod linux_x86_64;
pub mod commands;
pub mod logger;

32
src/constants.rs Normal file
View File

@ -0,0 +1,32 @@
#[derive(Debug)]
pub enum OpType {
Push,
Pop,
Minus,
Plus,
Print
}
#[derive(Debug)]
pub struct Operator {
pub typ: OpType,
pub value: i32,
}
impl Operator {
pub fn new(typ: OpType, value: i32) -> Self {
Self {
typ,
value
}
}
}
pub struct Token {
file: String,
line: u32,
col: u32,
text: String
}

View File

@ -0,0 +1,42 @@
use crate::constants::OpType;
use crate::interpret::logger;
use std::io::{self, Write};
use color_eyre::Result;
fn stack_pop(mut stack: &mut Vec<i32>) -> Result<i32, &'static str> {
match stack.pop() {
Some(i) => Ok(i),
None => Err("Stack underflow"),
}
}
pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
let mut stack: Vec<i32> = Vec::new();
for token in tokens {
match token.typ {
OpType::Push => {
stack.push(token.value);
},
OpType::Pop => {
stack.pop();
},
OpType::Plus => {
let a = stack_pop(&mut stack)?;
let b = stack_pop(&mut stack)?;
stack.push(b + a);
},
OpType::Minus => {
let a = stack_pop(&mut stack)?;
let b = stack_pop(&mut stack)?;
stack.push(b - a);
},
OpType::Print => {
let a = stack_pop(&mut stack)?;
println!("{a}");
// let _ = io::stdout().flush();
},
}
}
Ok(())
}

5
src/interpret/logger.rs Normal file
View File

@ -0,0 +1,5 @@
use crate::util::color;
pub fn error(msg: &str) {
println!("{red}error{r}: {msg}", red=color::FG_RED, r=color::RESET);
}

2
src/interpret/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod linux_x86_64;
mod logger;

28
src/lexer.rs Normal file
View File

@ -0,0 +1,28 @@
use crate::constants::Token;
use color_eyre::Result;
use crate::util::StringExtra;
pub fn strip_col(text: String, mut col: u32) -> Result<u32> {
while (col as usize) < text.len() && text.chars().nth(col as usize).unwrap().is_whitespace() {
col += 1;
}
Ok(32)
}
pub fn lex(code: String) -> Result<Vec<Token>> {
let lines: Vec<(usize, &str)> = code.split(['\n', '\r']).enumerate().collect();
let lines: Vec<(u32, String)> = lines.iter().map(|i| (i.0 as u32, i.1.to_string())).collect();
let mut tokens: Vec<Token> = Vec::new();
let mut col = strip_col(code, 0)?;
for line in lines {
let col_end = code.find_idx(' ', col);
col = strip_col(code, 0)?;
}
Ok(tokens)
}

62
src/main.rs Normal file
View File

@ -0,0 +1,62 @@
mod constants;
mod interpret;
mod util;
mod compile;
mod parser;
mod lexer;
use std::fs;
use color_eyre::Result;
use clap::Parser;
#[derive(Parser, Debug, Clone)]
#[command(author, version, about, long_about = None)]
pub struct Args {
/// Input source file
#[arg(long, short)]
in_file: String,
/// Output compiled file
#[arg(long, short, default_value_t=String::from("a.out"))]
out_file: String,
/// Compile
#[arg(long, short)]
compile: bool,
/// Interpert
#[arg(long, short='s')]
interpret: bool
}
use constants::{
OpType,
Operator
};
fn main() -> Result<(), &'static str> {
let args = Args::parse();
println!("MClang2 0.0.1");
let code = fs::read_to_string(&args.in_file).unwrap();
let tokens = lexer::lex(code);
dbg!(tokens);
return Ok(());
let mut parser = parser::Parser::new(code.clone());
let tokens = parser.parse()?;
if args.compile && args.interpret {
util::logger::error("Cannot compile and interpret at the same time");
} else if args.interpret {
interpret::linux_x86_64::run(tokens)?;
} else if args.compile {
if let Err(e) = compile::linux_x86_64::compile(tokens, args) {
println!("{}", e);
}
} else {
util::logger::error("Did not choose to compile or to interpret, exiting");
}
Ok(())
}

46
src/parser.rs Normal file
View File

@ -0,0 +1,46 @@
use crate::{constants::{Operator, OpType}, util};
use color_eyre::{Result, Report};
pub struct Parser {
file: String
}
impl Parser {
pub fn new(file: String) -> Self {
Self{
file
}
}
pub fn parse(&mut self) -> Result<Vec<Operator>, &'static str> {
let mut tokens = Vec::new();
for tok in self.file.split([' ', '\n', '\t', '\r']) {
if tok == "" {
continue;
}
match tok {
t if t.parse::<i32>().is_ok() => {
let num = t.parse::<i32>().unwrap();
tokens.push(Operator::new(OpType::Push, num));
},
"pop" => tokens.push(Operator::new(OpType::Pop, 0)),
"+" => tokens.push(Operator::new(OpType::Plus, 0)),
"-" => tokens.push(Operator::new(OpType::Minus, 0)),
"print" => tokens.push(Operator::new(OpType::Print, 0)),
t => {
util::logger::error("Unknown token '{t}'");
return Err("");
}
}
}
dbg!(&tokens);
Ok(tokens)
}
}

53
src/util.rs Normal file
View File

@ -0,0 +1,53 @@
pub mod color {
#[allow(dead_code)] pub const NONE: &str = "\x1b[0m";
#[allow(dead_code)] pub const RESET: &str = "\x1b[0m";
#[allow(dead_code)] pub const BRIGHT: &str = "\x1b[1m";
#[allow(dead_code)] pub const DIM: &str = "\x1b[2m";
#[allow(dead_code)] pub const UNDERSCORE: &str = "\x1b[4m";
#[allow(dead_code)] pub const BLINK: &str = "\x1b[5m";
#[allow(dead_code)] pub const REVERSE: &str = "\x1b[7m";
#[allow(dead_code)] pub const HIDDEN: &str = "\x1b[8m";
#[allow(dead_code)] pub const FG_BLACK: &str = "\x1b[30m";
#[allow(dead_code)] pub const FG_RED: &str = "\x1b[31m";
#[allow(dead_code)] pub const FG_GREEN: &str = "\x1b[32m";
#[allow(dead_code)] pub const FG_YELLOW: &str = "\x1b[33m";
#[allow(dead_code)] pub const FG_BLUE: &str = "\x1b[34m";
#[allow(dead_code)] pub const FG_MAGENTA: &str = "\x1b[35m";
#[allow(dead_code)] pub const FG_CYAN: &str = "\x1b[36m";
#[allow(dead_code)] pub const FG_WHITE: &str = "\x1b[37m";
#[allow(dead_code)] pub const BG_BLACK: &str = "\x1b[40m";
#[allow(dead_code)] pub const BG_RED: &str = "\x1b[41m";
#[allow(dead_code)] pub const BG_GREEN: &str = "\x1b[42m";
#[allow(dead_code)] pub const BG_YELLOW: &str = "\x1b[43m";
#[allow(dead_code)] pub const BG_BLUE: &str = "\x1b[44m";
#[allow(dead_code)] pub const BG_MAGENTA: &str = "\x1b[45m";
#[allow(dead_code)] pub const BG_CYAN: &str = "\x1b[46m";
#[allow(dead_code)] pub const BG_WHITE: &str = "\x1b[47m";
}
pub mod logger {
use crate::util::color;
pub fn error(msg: &str) {
println!("{red}error{r}: {msg}", red=color::FG_RED, r=color::RESET);
}
}
pub trait StringExtra{
fn find_idx(&self, pat: char, start: u32) -> u32;
}
impl StringExtra for String {
fn find_idx(&self, pat: char, start: u32) -> u32 {
let mut col = start;
for c in self.chars() {
if c == pat {
break;
}
col += 1;
}
col
}
}

1
test.mcl Normal file
View File

@ -0,0 +1 @@
35 34 + print