add the cool infinite variable thing

This commit is contained in:
abbie 2022-01-28 19:04:26 +00:00
parent a9fb5ccd26
commit daf79952e5
No known key found for this signature in database
GPG key ID: 04DDE463F9200F87
3 changed files with 381 additions and 104 deletions

View file

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
export debug="false" export debug="true"
export version="0.1.1" export version="0.2"
if [ $debug = "true" ]; then 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." 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."
sed -i "s/VERZON/$version-$(git rev-parse HEAD)/g" src/main.rs sed -i "s/VERZON/$version-$(git rev-parse HEAD)/g" src/main.rs

View file

@ -14,12 +14,17 @@ fn load_macro(set: String, mac: String, inputs: String, vars: Vec<String>) -> St
for input in based { for input in based {
jeff.push(input.to_string()) jeff.push(input.to_string())
} }
if contents.contains("[0]") {
if jeff[1].contains("![[hex]]") { if contents.contains("[") && contents.contains("]") {
let mut end = 0;
let mut cvar = 0;
let mut locofjeff = 1;
while end != 1 {
if jeff[locofjeff].contains("![[hex]]") {
contents = contents.replace("[0]", &format!("#{}",jeff[1])); contents = contents.replace("[0]", &format!("#{}",jeff[1]));
contents = contents.replace("![[hex]]", ""); contents = contents.replace("![[hex]]", "");
} else if jeff[1].contains("var!") { } else if jeff[locofjeff].contains("var!") {
let jazz: Vec<&str> = jeff[1].split("var!").collect(); let jazz: Vec<&str> = jeff[locofjeff].split("var!").collect();
let mut isin = false; let mut isin = false;
let mut i: u8 = 0; let mut i: u8 = 0;
for x in vars.to_vec() { for x in vars.to_vec() {
@ -27,97 +32,36 @@ fn load_macro(set: String, mac: String, inputs: String, vars: Vec<String>) -> St
isin = true; isin = true;
} }
if isin != true { if isin != true {
i = i + 1 i = i + 1;
} }
} }
if isin != true { if isin != true {
println!("ERROR: Reference to variable {} not assigned.", jazz[1]); println!("ERROR: Reference to variable {} not assigned.", jazz[1]);
process::exit(1); process::exit(1);
} }
contents = contents.replace("[0]", &format!(".{} LDZ",i.to_string())); contents = contents.replace(&format!("[{}]", cvar), &format!(".{} LDZ",i.to_string()));
contents = contents.replace("var!",""); contents = contents.replace("var!","");
} else { } else {
let mut zard: Vec<String> = [].to_vec(); let mut zard: Vec<String> = [].to_vec();
for n in jeff[1].chars() { for n in jeff[locofjeff].chars() {
let chard = str_to_hex(n); let chard = str_to_hex(n);
zard.push(contents.replace("[0]", &format!("#{}", chard))) zard.push(contents.replace(&format!("[{}]", cvar), &format!("#{}",chard)));
} }
for charl in zard { for charl in zard {
keg.push_str(&format!("{}",&charl)); keg.push_str(&format!("{}",&charl));
} }
contents = keg.to_string(); contents = keg.to_string();
} }
} if contents.contains(&format!("[{}]", cvar + 1)) {
if contents.contains("[1]") { cvar = cvar + 1;
coont = 1; locofjeff = locofjeff + 2;
if jeff[3].contains("![[hex]]") {
contents = contents.replace("[1]", &format!("#{}",jeff[3]));
contents = contents.replace("![[hex]]", "");
} else if jeff[3].contains("var!") {
let jazz: Vec<&str> = jeff[3].split("var!").collect();
let mut isin = false;
let mut i: u8 = 0;
for x in vars.to_vec() {
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("[1]", &format!(".{} LDZ",i.to_string()));
contents = contents.replace("var!", "");
} else { } else {
let mut zard: Vec<String> = [].to_vec(); end = 1;
for n in jeff[3].chars() {
let chard = str_to_hex(n);
zard.push(contents.replace("[1]", &format!("#{}", chard)))
}
for charl in zard {
keg.push_str(&format!("{}", &charl));
}
contents = keg.to_string();
}
}
if contents.contains("[2]") {
coont = 2; coont = 2;
if jeff[5].contains("![[hex]]") {
contents = contents.replace("[2]", &format!("#{}",jeff[5]));
contents = contents.replace("![[hex]]", "");
} else if jeff[5].contains("var!") {
let jazz: Vec<&str> = jeff[5].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("[2]", &format!(".{} LDZ",i.to_string()));
contents = contents.replace("var!", "");
} else {
let mut zard: Vec<String> = [].to_vec();
for n in jeff[5].chars() {
let chard = str_to_hex(n);
zard.push(contents.replace("[2]", &format!("#{}", chard)))
}
for charl in zard {
keg.push_str(&format!("{}", &charl));
}
contents = keg.to_string();
}
} }
if coont != 1 { if coont != 1 {
let jazz: Vec<&str> = contents.split("\n").collect(); let jazz: Vec<&str> = contents.split("\n").collect();
let mut xcont = "".to_string(); let mut xcont = "".to_string();
@ -172,11 +116,25 @@ fn parse(code: String) -> String {
} else if line.contains("var.") { } else if line.contains("var.") {
let name: Vec<&str> = line.split("var.").collect(); let name: Vec<&str> = line.split("var.").collect();
let value: Vec<&str> = name[1].split("=").collect(); let value: Vec<&str> = name[1].split("=").collect();
if vars.contains(&value[0].to_string()) {
blocks[2].push_str(&format!("@{}\n",counter.to_string())); blocks[2].push_str(&format!("@{}\n",counter.to_string()));
vars.push(format!("{}",value[0])); vars.push(format!("{}",value[0]));
blocks[3].push_str(&format!("LIT {} .{} STZ\n", value[1], counter.to_string())); blocks[3].push_str(&format!("LIT {} .{} STZ\n", value[1], counter.to_string()));
counter = counter + 1; counter = counter + 1;
println!("Assigned variable {}.", value[0]); } else {
let mut isin = false;
let mut i: u8 = 0;
for x in vars.to_vec() {
if x == value[0].to_string() {
isin = true;
}
if isin != true {
i = i + 1;
}
}
blocks[3].push_str(&format!("LIT {} .{} STZ\n", value[1], counter.to_string()));
}
println!("Assigned value to variable {}.", value[0]);
} else if line.contains(";;") { } else if line.contains(";;") {
let tmac: Vec<&str> = line.split(";;").collect(); let tmac: Vec<&str> = line.split(";;").collect();
let brack: Vec<&str> = tmac.to_vec()[1].split("(").collect(); let brack: Vec<&str> = tmac.to_vec()[1].split("(").collect();

319
src/main.rs.save Normal file
View file

@ -0,0 +1,319 @@
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();
let mut coont = 0;
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.to_vec() {
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();
}
}
if contents.contains("[1]") {
coont = 1;
if jeff[3].contains("![[hex]]") {
contents = contents.replace("[1]", &format!("#{}",jeff[3]));
contents = contents.replace("![[hex]]", "");
} else if jeff[3].contains("var!") {
let jazz: Vec<&str> = jeff[3].split("var!").collect();
let mut isin = false;
let mut i: u8 = 0;
for x in vars.to_vec() {
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("[1]", &format!(".{} LDZ",i.to_string()));
contents = contents.replace("var!", "");
} else {
let mut zard: Vec<String> = [].to_vec();
for n in jeff[3].chars() {
let chard = str_to_hex(n);
zard.push(contents.replace("[1]", &format!("#{}", chard)))
}
for charl in zard {
keg.push_str(&format!("{}", &charl));
}
contents = keg.to_string();
}
}
if contents.contains("[2]") {
coont = 2;
if jeff[5].contains("![[hex]]") {
contents = contents.replace("[2]", &format!("#{}",jeff[5]));
contents = contents.replace("![[hex]]", "");
} else if jeff[5].contains("var!") {
let jazz: Vec<&str> = jeff[5].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("[2]", &format!(".{} LDZ",i.to_string()));
contents = contents.replace("var!", "");
} else {
let mut zard: Vec<String> = [].to_vec();
for n in jeff[5].chars() {
let chard = str_to_hex(n);
zard.push(contents.replace("[2]", &format!("#{}", chard)))
}
for charl in zard {
keg.push_str(&format!("{}", &charl));
}
contents = keg.to_string();
}
}
if coont != 1 {
let jazz: Vec<&str> = contents.split("\n").collect();
let mut xcont = "".to_string();
for x in jazz {
if x.contains("[") == false && x.contains("]") == false {
xcont = format!("{}\n{}",xcont,x);
}
}
contents = xcont.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();
}