add 'mem' and extent Token to have Token.jmp which is used in if, else, end, while blocks instead of Token.value

This commit is contained in:
MCorange 2023-03-14 19:01:06 +02:00
parent 5ec182ab58
commit 782c3aadde
20 changed files with 2164 additions and 55 deletions

BIN
editor/mclang-0.0.1.vsix Normal file

Binary file not shown.

2
editor/vscode/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
**/*.log
**/node_modules/

17
editor/vscode/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
// A launch configuration that launches the extension inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
]
}
]
}

View File

@ -0,0 +1,4 @@
.vscode/**
.vscode-test/**
.gitignore
vsc-extension-quickstart.md

View File

@ -0,0 +1,9 @@
# Change Log
All notable changes to the "mclang" extension will be documented in this file.
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
## [Unreleased]
- Initial release

17
editor/vscode/README.md Normal file
View File

@ -0,0 +1,17 @@
# mclang README
Code highlghting for mclang 1 and 2
## Known Issues
None
## Release Notes
Users appreciate release notes as you update your extension.
### 1.0.0
Initial release of mclang
**Enjoy!**

View File

@ -0,0 +1,30 @@
{
"comments": {
// symbol used for single line comment. Remove this entry if your language does not support line comments
"lineComment": "//",
// symbols used for start and end a block comment. Remove this entry if your language does not support block comments
"blockComment": [ "/*", "*/" ]
},
// symbols used as brackets
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
],
// symbols that are auto closed when typing
"autoClosingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"]
],
// symbols that can be used to surround a selection
"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"]
]
}

View File

@ -0,0 +1,38 @@
{
"name": "mclang",
"displayName": "mclang",
"description": "Code highlighting for mclang",
"version": "0.0.1",
"engines": {
"vscode": "^1.54.0"
},
"categories": [
"Programming Languages"
],
"contributes": {
"languages": [
{
"id": "mclang",
"aliases": [
"MCLang",
"mclang"
],
"extensions": [
".mcl"
],
"configuration": "./language-configuration.json"
}
],
"grammars": [
{
"language": "mclang",
"scopeName": "source.mcl",
"path": "./syntaxes/mclang.tmLanguage.json"
}
]
},
"dependencies": {
"generator-code": "^1.7.4",
"vsce": "^2.15.0"
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,29 @@
# Welcome to your VS Code Extension
## What's in the folder
* This folder contains all of the files necessary for your extension.
* `package.json` - this is the manifest file in which you declare your language support and define the location of the grammar file that has been copied into your extension.
* `syntaxes/mclang.tmLanguage.json` - this is the Text mate grammar file that is used for tokenization.
* `language-configuration.json` - this is the language configuration, defining the tokens that are used for comments and brackets.
## Get up and running straight away
* Make sure the language configuration settings in `language-configuration.json` are accurate.
* Press `F5` to open a new window with your extension loaded.
* Create a new file with a file name suffix matching your language.
* Verify that syntax highlighting works and that the language configuration settings are working.
## Make changes
* You can relaunch the extension from the debug toolbar after making changes to the files listed above.
* You can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes.
## Add more language features
* To add features such as IntelliSense, hovers and validators check out the VS Code extenders documentation at https://code.visualstudio.com/docs
## Install your extension
* To start using your extension with Visual Studio Code copy it into the `<user home>/.vscode/extensions` folder and restart Code.
* To share your extension with the world, read on https://code.visualstudio.com/docs about publishing an extension.

1784
editor/vscode/yarn.lock Normal file

File diff suppressed because it is too large Load Diff

View File

@ -61,6 +61,7 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, "addr_{}:", ti)?; writeln!(writer, "addr_{}:", ti)?;
match token.typ { match token.typ {
// stack
OpType::Push => { OpType::Push => {
writeln!(writer, " ; -- PUSH {}", token.value)?; writeln!(writer, " ; -- PUSH {}", token.value)?;
writeln!(writer, " mov rax, {}", token.value)?; writeln!(writer, " mov rax, {}", token.value)?;
@ -73,6 +74,30 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, " pop")?; writeln!(writer, " pop")?;
ti += 1; ti += 1;
}, },
OpType::Print => {
writeln!(writer, " ; -- PRINT")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " call print")?;
ti += 1;
},
OpType::Dup => {
writeln!(writer, " ; -- DUP")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " push rax")?;
writeln!(writer, " push rax")?;
ti += 1;
},
//mem
OpType::Mem => {
writeln!(writer, " ; -- MEM")?;
writeln!(writer, " push mem")?;
ti += 1;
}
// math
OpType::Plus => { OpType::Plus => {
writeln!(writer, " ; -- PLUS")?; writeln!(writer, " ; -- PLUS")?;
writeln!(writer, " pop rax")?; writeln!(writer, " pop rax")?;
@ -102,7 +127,7 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
}, },
OpType::Lt => { OpType::Lt => {
writeln!(writer, " ; -- EQUALS")?; writeln!(writer, " ; -- LT")?;
writeln!(writer, " mov rcx, 0")?; writeln!(writer, " mov rcx, 0")?;
writeln!(writer, " mov rdx, 1")?; writeln!(writer, " mov rdx, 1")?;
writeln!(writer, " pop rbx")?; writeln!(writer, " pop rbx")?;
@ -114,7 +139,7 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
}, },
OpType::Gt => { OpType::Gt => {
writeln!(writer, " ; -- EQUALS")?; writeln!(writer, " ; -- GT")?;
writeln!(writer, " mov rcx, 0")?; writeln!(writer, " mov rcx, 0")?;
writeln!(writer, " mov rdx, 1")?; writeln!(writer, " mov rdx, 1")?;
writeln!(writer, " pop rbx")?; writeln!(writer, " pop rbx")?;
@ -125,32 +150,18 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
ti += 1; ti += 1;
}, },
OpType::Print => {
writeln!(writer, " ; -- PRINT")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " call print")?;
ti += 1;
},
OpType::Dup => {
writeln!(writer, " ; -- DUP")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " push rax")?;
writeln!(writer, " push rax")?;
ti += 1;
},
// block
OpType::If => { OpType::If => {
writeln!(writer, " ; -- IF")?; writeln!(writer, " ; -- IF")?;
writeln!(writer, " pop rax")?; writeln!(writer, " pop rax")?;
writeln!(writer, " test rax, rax")?; writeln!(writer, " test rax, rax")?;
writeln!(writer, " jz addr_{}", token.value + 1)?; writeln!(writer, " jz addr_{}", token.jmp)?;
ti += 1; ti += 1;
}, },
OpType::Else => { OpType::Else => {
writeln!(writer, " ; -- ELSE")?; writeln!(writer, " ; -- ELSE")?;
writeln!(writer, " jmp addr_{}", token.value)?; writeln!(writer, " jmp addr_{}", token.jmp)?;
ti += 1; ti += 1;
}, },
OpType::While => { OpType::While => {
@ -161,12 +172,12 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, " ; -- DO")?; writeln!(writer, " ; -- DO")?;
writeln!(writer, " pop rax")?; writeln!(writer, " pop rax")?;
writeln!(writer, " test rax, rax")?; writeln!(writer, " test rax, rax")?;
writeln!(writer, " jz addr_{}", token.value)?; writeln!(writer, " jz addr_{}", token.jmp)?;
ti += 1; ti += 1;
} }
OpType::End => { OpType::End => {
writeln!(writer, " ; -- END")?; writeln!(writer, " ; -- END")?;
writeln!(writer, " jmp addr_{}", token.value)?; writeln!(writer, " jmp addr_{}", token.jmp)?;
ti += 1; ti += 1;
}, },
} }
@ -175,6 +186,10 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, " mov rax, 60")?; writeln!(writer, " mov rax, 60")?;
writeln!(writer, " mov rdi, 0")?; writeln!(writer, " mov rdi, 0")?;
writeln!(writer, " syscall")?; writeln!(writer, " syscall")?;
writeln!(writer, "segment .bss")?;
writeln!(writer, " mem: resb {}", crate::compile::MEM_SZ)?;
writer.flush()?; writer.flush()?;
linux_x86_64_compile_and_link(of_a, of_o, of_c)?; linux_x86_64_compile_and_link(of_a, of_o, of_c)?;
Ok(()) Ok(())

View File

@ -1,3 +1,4 @@
pub mod linux_x86_64; pub mod linux_x86_64;
pub mod commands; pub mod commands;
pub const MEM_SZ: u32 = 640 * 1000; // 4kb

View File

@ -14,7 +14,8 @@ pub enum OpType {
Gt, Gt,
Lt, Lt,
While, While,
Do Do,
Mem
} }
@ -27,6 +28,7 @@ pub enum OpType {
pub struct Operator { pub struct Operator {
pub typ: OpType, pub typ: OpType,
pub value: i32, pub value: i32,
pub jmp: i32,
pub pos: (String, u32, u32) pub pos: (String, u32, u32)
} }
@ -35,6 +37,7 @@ impl Operator {
Self { Self {
typ, typ,
value, value,
jmp: 0,
pos: (file, row, col) pos: (file, row, col)
} }
} }

View File

@ -12,10 +12,12 @@ fn stack_pop(stack: &mut Vec<i32>) -> Result<i32, &'static str> {
pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
let mut stack: Vec<i32> = Vec::new(); let mut stack: Vec<i32> = Vec::new();
let mut ti = 0; let mut ti = 0;
let mut mem: [u8; 16*1024];
while ti < tokens.len() { while ti < tokens.len() {
let token = &tokens[ti]; let token = &tokens[ti];
match token.typ { match token.typ {
// stack
OpType::Push => { OpType::Push => {
stack.push(token.value); stack.push(token.value);
ti += 1; ti += 1;
@ -24,6 +26,27 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
stack.pop(); stack.pop();
ti += 1; ti += 1;
}, },
OpType::Dup => {
let a = stack_pop(&mut stack)?;
stack.push(a);
stack.push(a);
ti += 1;
},
OpType::Print => {
let a = stack_pop(&mut stack)?;
println!("{a}");
// let _ = io::stdout().flush();
ti += 1;
},
// mem
OpType::Mem => {
stack.push(0);
ti += 1;
}
// math
OpType::Plus => { OpType::Plus => {
let a = stack_pop(&mut stack)?; let a = stack_pop(&mut stack)?;
let b = stack_pop(&mut stack)?; let b = stack_pop(&mut stack)?;
@ -55,30 +78,17 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
ti += 1; ti += 1;
}, },
OpType::Print => { // blocks
let a = stack_pop(&mut stack)?;
println!("{a}");
// let _ = io::stdout().flush();
ti += 1;
},
OpType::Dup => {
let a = stack_pop(&mut stack)?;
stack.push(a);
stack.push(a);
ti += 1;
},
OpType::If => { OpType::If => {
let a = stack_pop(&mut stack)?; let a = stack_pop(&mut stack)?;
if a == 0 { if a == 0 {
ti = (token.value + 1) as usize; ti = (token.jmp) as usize;
} else { } else {
ti += 1; ti += 1;
} }
}, },
OpType::Else => { OpType::Else => {
ti = token.value as usize; ti = token.jmp as usize;
}, },
OpType::While => { OpType::While => {
@ -87,13 +97,13 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
OpType::Do => { OpType::Do => {
let a = stack.pop().unwrap(); let a = stack.pop().unwrap();
if a == 0 { if a == 0 {
ti = token.value as usize; ti = token.jmp as usize;
} else { } else {
ti += 1; ti += 1;
} }
} }
OpType::End => { OpType::End => {
ti = token.value as usize; ti = token.jmp as usize;
} }
} }
} }

View File

@ -30,7 +30,12 @@ fn lex_line(text: String) -> Result<Vec<(u32, String)>> {
} }
pub fn lex(code: String, file: &String) -> Result<Vec<Token>> { pub fn lex(code: String, file: &String) -> Result<Vec<Token>> {
let lines: Vec<(usize, &str)> = code.split(['\n', '\r']).enumerate().collect(); let lines: Vec<(usize, &str)> = code
.split("//").collect::<Vec<&str>>()[0]
.split(['\n', '\r'])
.enumerate()
.collect();
let lines: Vec<(u32, String)> = lines.iter().map(|i| (i.0 as u32, i.1.to_string())).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 tokens: Vec<Token> = Vec::new();

View File

@ -14,7 +14,7 @@ use clap::Parser;
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
pub struct Args { pub struct Args {
/// Input source file /// Input source file
#[arg(long, short)] #[arg(long, short)]
in_file: String, in_file: String,
/// Output compiled file /// Output compiled file

View File

@ -17,7 +17,7 @@ pub fn cross_ref(mut tokens: Vec<Operator>) -> Vec<Operator> {
std::process::exit(1); // idc std::process::exit(1); // idc
} }
(*if_og).value = (ip + 1) as i32; (*if_og).jmp = (ip + 1) as i32;
stack.push(ip as u32); stack.push(ip as u32);
}, },
OpType::End => { OpType::End => {
@ -25,21 +25,21 @@ pub fn cross_ref(mut tokens: Vec<Operator>) -> Vec<Operator> {
let mut block_og = &mut tokens[block_ip as usize].clone(); let mut block_og = &mut tokens[block_ip as usize].clone();
if vec![OpType::If, OpType::Else].contains(&(*block_og).typ) { if vec![OpType::If, OpType::Else].contains(&(*block_og).typ) {
(*block_og).value = ip as i32; (*block_og).jmp = ip as i32;
tokens[block_ip as usize] = block_og.clone(); tokens[block_ip as usize] = block_og.clone();
let do_og = &mut tokens[ip as usize].clone(); let do_og = &mut tokens[ip as usize].clone();
do_og.value = (ip + 1) as i32; do_og.jmp = (ip + 1) as i32;
tokens[ip as usize] = (*do_og).clone(); tokens[ip as usize] = (*do_og).clone();
} else if (*block_og).typ == OpType::Do { } else if (*block_og).typ == OpType::Do {
let do_og = &mut tokens[ip as usize]; let do_og = &mut tokens[ip as usize];
do_og.value = block_og.value; do_og.jmp = block_og.jmp;
tokens[ip as usize] = (*do_og).clone(); tokens[ip as usize] = (*do_og).clone();
let mut block_og = block_og.clone(); let mut block_og = block_og.clone();
block_og.value = (ip + 1) as i32; block_og.jmp = (ip + 1) as i32;
tokens[block_ip as usize] = block_og.clone(); tokens[block_ip as usize] = block_og.clone();
} else { } else {
util::logger::pos_error(op.clone().pos,"'end' can only close 'if' blocks"); util::logger::pos_error(op.clone().pos,"'end' can only close 'if' blocks");
@ -52,7 +52,7 @@ pub fn cross_ref(mut tokens: Vec<Operator>) -> Vec<Operator> {
} }
OpType::Do => { OpType::Do => {
let while_ip = stack.pop().unwrap(); let while_ip = stack.pop().unwrap();
(&mut tokens[ip as usize]).value = while_ip as i32; (&mut tokens[ip as usize]).jmp = while_ip as i32;
stack.push(ip as u32); stack.push(ip as u32);
} }
_ => () _ => ()
@ -91,7 +91,7 @@ impl Parser {
// stack // stack
"dup" => tokens.push(Operator::new(OpType::Dup, 0, token.file.clone(), token.line, token.col)), "dup" => tokens.push(Operator::new(OpType::Dup, 0, token.file.clone(), token.line, token.col)),
"pop" => tokens.push(Operator::new(OpType::Pop, 0, token.file.clone(), token.line, token.col)), "drop" => tokens.push(Operator::new(OpType::Pop, 0, token.file.clone(), token.line, token.col)),
// comp and math // comp and math
"+" => tokens.push(Operator::new(OpType::Plus, 0, token.file.clone(), token.line, token.col)), "+" => tokens.push(Operator::new(OpType::Plus, 0, token.file.clone(), token.line, token.col)),
@ -106,6 +106,7 @@ impl Parser {
"end" => tokens.push(Operator::new(OpType::End, 0, token.file.clone(), token.line, token.col)), "end" => tokens.push(Operator::new(OpType::End, 0, token.file.clone(), token.line, token.col)),
"while" => tokens.push(Operator::new(OpType::While, 0, token.file.clone(), token.line, token.col)), "while" => tokens.push(Operator::new(OpType::While, 0, token.file.clone(), token.line, token.col)),
"do" => tokens.push(Operator::new(OpType::Do, 0, token.file.clone(), token.line, token.col)), "do" => tokens.push(Operator::new(OpType::Do, 0, token.file.clone(), token.line, token.col)),
"mem" => tokens.push(Operator::new(OpType::Mem, 0, token.file.clone(), token.line, token.col)),

View File

@ -1,6 +1 @@
10 while dup 0 > do mem print
dup print
1 -
end
696969 print

6
test13.mcl Normal file
View File

@ -0,0 +1,6 @@
//we dont have text yet, its so early stages still
//cant it do putchar(getchar())
// not yet
// ;-; ;-; ;-; ;-; ;-;
// ping me on dsc
//..., im working on it, rn im gonna make elif i think, you can watch, lemme clean my table gimme 5 mins