Add base repo [SIGNED]

This commit is contained in:
abbie 2022-01-27 08:01:45 +00:00
commit 668ce74ea2
No known key found for this signature in database
GPG key ID: 04DDE463F9200F87
5 changed files with 261 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "lintc"
version = "0.1.0"

8
Cargo.toml Normal file
View file

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

7
build.sh Normal file
View file

@ -0,0 +1,7 @@
#!/bin/sh
export debug="true"
export version="0.1"
if [ $debug = "true" ]; then
echo "NOTE: This is going to be a development build. Please make sure you downloaded the point version from the 'about' tab if you wanted a stabler version."
fi

238
src/main.rs Normal file
View file

@ -0,0 +1,238 @@
use std::io::prelude::*;
use std::fs::File;
use std::env;
use std::process;
fn load_macro(set: String, mac: String, inputs: String, vars: Vec<String>) -> String {
let mut file = File::open(format!("macros/{}/{}", set, mac)).expect("Unable to open macro");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Unable to read macro");
let based: Vec<&str> = inputs.split("\"").collect();
let mut jeff: Vec<String> = [].to_vec();
let mut keg: String = "".to_owned();
for input in based {
jeff.push(input.to_string())
}
if contents.contains("[0]") {
if jeff[1].contains("![[hex]]") {
contents = contents.replace("[0]", &format!("#{}",jeff[1]));
contents = contents.replace("![[hex]]", "");
} else if jeff[1].contains("var!") {
let jazz: Vec<&str> = jeff[1].split("var!").collect();
let mut isin = false;
let mut i: u8 = 0;
for x in vars {
if x == jazz[1].to_string() {
isin = true;
}
if isin != true {
i = i + 1
}
}
if isin != true {
println!("ERROR: Reference to variable {} not assigned.", jazz[1]);
process::exit(1);
}
contents = contents.replace("[0]", &format!(".{} LDZ",i.to_string()));
contents = contents.replace("var!", "");
} else {
let mut zard: Vec<String> = [].to_vec();
for n in jeff[1].chars() {
let chard = str_to_hex(n);
zard.push(contents.replace("[0]", &format!("#{}", chard)))
}
for charl in zard {
keg.push_str(&format!("{}", &charl));
}
contents = keg.to_string();
}
}
return contents.to_string();
}
fn determine_device(line: String) -> String {
let mut device = "";
if line.contains("console") { device = "|10 @Console [ &vector $2 &read $1 &pad $5 &write $1 &error $1 ]"; }
else if line.contains("system") { device = "|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]"; }
else if line.contains("screen") { device = "|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]"; }
else if line.contains("controller") { device = "|80 @Controller [ &vector $2 &button $1 &key $1 ]"; }
else if line.contains("mouse") { device = "|90 @Mouse [ &vector $2 &x $2 &y $2 &state $1 &pad $3 &scrollx $2 &scrolly $2 ]"; }
return device.to_string();
}
fn load_source(filename: String) -> String {
let mut file = File::open(filename).expect("Unable to open file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Unable to read file");
return contents;
}
fn parse(code: String) -> String {
// blocks[0] = Devices block
// blocks[1] = User-defined macros [TODO]
// blocks[2] = Zero page
// blocks[3] = Main code block
let version = "VERZON"; // don't touch this ever.
let mut vars: Vec<String> = [].to_vec(); // Variables and vectors in order.
let mut blocks: Vec<String> = ["".to_string(), "".to_string(), "".to_string(), "".to_string()].to_vec();
let replaced = code.replace("\n\n", "\n");
let parsed: Vec<&str> = replaced.split("\n").collect();
let _genr: Vec<String> = [].to_vec();
let mut macrs: Vec<String> = [].to_vec();
let mut counter: u8 = 0; // Counter for assignment of variables and vectors.
for line in parsed {
if line.contains("use device") {
let insert = determine_device(line.to_string());
blocks[0].push_str(&insert);
println!("Determined and added use device line");
} else if line.contains("use macro") {
let name = line.replace("use macro ","");
println!("Enabled macro set {}", name);
macrs.push(name);
} else if line.contains("var.") {
let name: Vec<&str> = line.split("var.").collect();
let value: Vec<&str> = name[1].split("=").collect();
blocks[2].push_str(&format!("@{}\n",counter.to_string()));
vars.push(format!("{}",value[0]));
blocks[3].push_str(&format!("LIT {} .{} STZ\n", value[1], counter.to_string()));
counter = counter + 1;
println!("Assigned variable {}.", value[0]);
} else if line.contains(";;") {
let tmac: Vec<&str> = line.split(";;").collect();
let brack: Vec<&str> = tmac.to_vec()[1].split("(").collect();
let _sbrack: Vec<&str> = brack.to_vec()[1].split(")").collect();
println!("Inserting macro {} from {}", brack[0], tmac[0]);
if macrs.contains(&format!("{}", &tmac[0])) {
blocks[3].push_str(&load_macro(tmac[0].to_string(), brack[0].to_string(), brack[1].to_string(), vars.to_vec()));
} else {
println!("ERROR: Macro set {} not initalised in code.", tmac[0]);
process::exit(1);
}
}
}
return format!("(generated by lintc {})\n{}\n{}\n|0000\n{}\n|0100\n{}", version, blocks[0], blocks[1], blocks[2], blocks[3]).to_string();
}
fn main() {
let args: Vec<String> = env::args().collect();
println!("Reading file {}", &args[1]);
let code = load_source(args[1].to_string());
println!("Processing input.");
let parsed = parse(code);
println!("Saving output.");
let qargs: Vec<String> = env::args().collect();
let test = qargs[1].replace("lnt","tal");
let mut file = File::create(format!("{}", test)).expect("ERROR: File could not be created.");
file.write_all(parsed.as_bytes()).expect("ERROR: Data could not be written to file.");
println!("Compilation complete. Output saved at {}.", test);
}
fn str_to_hex(n: char) -> String {
// This function is very much spaghetti.
// I currently can't think of a better way to do this.
// If you are an exorcist, please do your best.
// If you are a keen programmer, please email me a patch to
// replace this god-forsaken function.
// It has been shamed by living at the bottom of this file.
let k = n.to_string();
let mut l = k.to_string();
if k.contains("3") { l = k.replace("3","33"); }
if k.contains("0") { l = k.replace("0","30"); }
if k.contains("1") { l = k.replace("1","31"); }
if k.contains("2") { l = k.replace("2","32"); }
if k.contains("4") { l = k.replace("4","34"); }
if k.contains("5") { l = k.replace("5","35"); }
if k.contains("6") { l = k.replace("6","36"); }
if k.contains("7") { l = k.replace("7","37"); }
if k.contains("8") { l = k.replace("8","38"); }
if k.contains("9") { l = k.replace("9","39"); }
if k.contains(" ") { l = k.replace(" ","20"); }
if k.contains("!") { l = k.replace("!","21"); }
if k.contains("\"") { l = k.replace("\"","22"); }
if k.contains("#") { l = k.replace("#","23"); }
if k.contains("$") { l = k.replace("$","24"); }
if k.contains("%") { l = k.replace("%","25"); }
if k.contains("&") { l = k.replace("&","26"); }
if k.contains("'") { l = k.replace("'","27"); }
if k.contains("(") { l = k.replace("(","28"); }
if k.contains(")") { l = k.replace(")","29"); }
if k.contains("*") { l = k.replace("*","2a"); }
if k.contains("+") { l = k.replace("+","2b"); }
if k.contains(",") { l = k.replace(",","2c"); }
if k.contains("-") { l = k.replace("-","2d"); }
if k.contains(".") { l = k.replace(".","2e"); }
if k.contains("/") { l = k.replace("/","2f"); }
if k.contains(":") { l = k.replace(":","3a"); }
if k.contains(";") { l = k.replace(";","3b"); }
if k.contains("<") { l = k.replace("<","3c"); }
if k.contains("=") { l = k.replace("=","3d"); }
if k.contains(">") { l = k.replace(">","3e"); }
if k.contains("?") { l = k.replace("?","3f"); }
if k.contains("@") { l = k.replace("@","40"); }
if k.contains("A") { l = k.replace("A","41"); }
if k.contains("B") { l = k.replace("B","42"); }
if k.contains("C") { l = k.replace("C","43"); }
if k.contains("D") { l = k.replace("D","44"); }
if k.contains("E") { l = k.replace("E","45"); }
if k.contains("F") { l = k.replace("F","46"); }
if k.contains("G") { l = k.replace("G","47"); }
if k.contains("H") { l = k.replace("H","48"); }
if k.contains("I") { l = k.replace("I","49"); }
if k.contains("J") { l = k.replace("J","4a"); }
if k.contains("K") { l = k.replace("K","4b"); }
if k.contains("L") { l = k.replace("L","4c"); }
if k.contains("M") { l = k.replace("M","4d"); }
if k.contains("N") { l = k.replace("N","4e"); }
if k.contains("O") { l = k.replace("O","4f"); }
if k.contains("P") { l = k.replace("P","50"); }
if k.contains("Q") { l = k.replace("Q","51"); }
if k.contains("R") { l = k.replace("R","52"); }
if k.contains("S") { l = k.replace("S","53"); }
if k.contains("T") { l = k.replace("T","54"); }
if k.contains("U") { l = k.replace("U","55"); }
if k.contains("V") { l = k.replace("V","56"); }
if k.contains("W") { l = k.replace("W","57"); }
if k.contains("X") { l = k.replace("X","58"); }
if k.contains("Y") { l = k.replace("Y","59"); }
if k.contains("Z") { l = k.replace("Z","5a"); }
if k.contains("[") { l = k.replace("[","5b"); }
if k.contains("\\") { l = k.replace("\\","5c"); }
if k.contains("]") { l = k.replace("]","5d"); }
if k.contains("^") { l = k.replace("^","5e"); }
if k.contains("_") { l = k.replace("_","5f"); }
if k.contains("`") { l = k.replace("`","60"); }
if k.contains("a") { l = k.replace("a","61"); }
if k.contains("b") { l = k.replace("b","62"); }
if k.contains("c") { l = k.replace("c","63"); }
if k.contains("d") { l = k.replace("d","64"); }
if k.contains("e") { l = k.replace("e","65"); }
if k.contains("f") { l = k.replace("f","66"); }
if k.contains("g") { l = k.replace("g","67"); }
if k.contains("h") { l = k.replace("h","68"); }
if k.contains("i") { l = k.replace("i","69"); }
if k.contains("j") { l = k.replace("j","6a"); }
if k.contains("k") { l = k.replace("k","6b"); }
if k.contains("l") { l = k.replace("l","6c"); }
if k.contains("m") { l = k.replace("m","6d"); }
if k.contains("n") { l = k.replace("n","6e"); }
if k.contains("o") { l = k.replace("o","6f"); }
if k.contains("p") { l = k.replace("p","70"); }
if k.contains("q") { l = k.replace("q","71"); }
if k.contains("r") { l = k.replace("r","72"); }
if k.contains("s") { l = k.replace("s","73"); }
if k.contains("t") { l = k.replace("t","74"); }
if k.contains("u") { l = k.replace("u","75"); }
if k.contains("v") { l = k.replace("v","76"); }
if k.contains("w") { l = k.replace("w","77"); }
if k.contains("x") { l = k.replace("x","78"); }
if k.contains("y") { l = k.replace("y","79"); }
if k.contains("z") { l = k.replace("z","7a"); }
if k.contains("{") { l = k.replace("{","7b"); }
if k.contains("|") { l = k.replace("|","7c"); }
if k.contains("}") { l = k.replace("}","7d"); }
if k.contains("~") { l = k.replace("~","7e"); }
return l.to_string();
}