diff --git a/Cargo.lock b/Cargo.lock index e74f4ae..c7281e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,16 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "ONLang" -version = "0.1.0" -dependencies = [ - "clap", - "json5", - "serde", - "serde_json", -] - [[package]] name = "atty" version = "0.2.14" @@ -89,6 +79,17 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + [[package]] name = "cpufeatures" version = "0.2.2" @@ -176,6 +177,12 @@ dependencies = [ "serde", ] +[[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.126" @@ -188,6 +195,17 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +[[package]] +name = "onlang" +version = "0.1.0" +dependencies = [ + "clap", + "colored", + "json5", + "serde", + "serde_json", +] + [[package]] name = "os_str_bytes" version = "6.2.0" diff --git a/Cargo.toml b/Cargo.toml index 3bb7144..1912b3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ONLang" +name = "onlang" version = "0.1.0" edition = "2021" @@ -9,4 +9,5 @@ edition = "2021" serde = { version = "1.0", features = ["derive"] } json5 = "0.4.1" serde_json = "1.0" -clap = { version = "3.2", features = ["derive"] } \ No newline at end of file +clap = { version = "3.2", features = ["derive"] } +colored = "2" \ No newline at end of file diff --git a/src/interpreter.rs b/src/interpreter.rs index 5ff6b76..8b56e1d 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1,6 +1,7 @@ +use colored::*; +use json5; use serde_json::Value; use std::collections::HashMap; - pub struct Interpreter { input: String, vars: HashMap, @@ -12,7 +13,7 @@ impl Interpreter { Interpreter { input, vars: HashMap::new(), - pos: 0, + pos: 1, } } @@ -21,37 +22,117 @@ impl Interpreter { let arr = obj.as_array_mut().unwrap(); for command in arr { - for (name, value) in command.as_object().unwrap() { - if name == "print" { - self.print(value); + match command { + Value::Object(command) => { + for (name, value) in command { + match name.as_str() { + "print" => match value { + Value::Array(value) => { + self.print(value, false); + } + Value::String(value) => { + self.print(&mut vec![Value::String(value.to_string())], false); + } + _ => { + self.error("Unsupported data type for the print argument"); + } + }, + "println" => match value { + Value::Array(value) => { + self.print(value, true); + } + Value::String(value) => { + self.print(&mut vec![Value::String(value.to_string())], true); + } + _ => { + self.error("Unsupported data type for the println argument"); + } + }, + name => { + self.unk_token(&name); + } + } + } + } + Value::String(name) => match name.as_str() { + "Exit" => { + self.exit(); + } + "ErrExit" => { + self.err_exit(); + } + value => { + self.print(&mut vec![Value::String(value.to_string())], false); + } + }, + Value::Array(command) => { + self.print(command, false); + } + _ => { + self.error("Unsupported data type for the command"); } } + + self.pos += 1; } } - - fn print(&self, args: &Value) { - for arg in args.as_array().unwrap() { + fn print(&self, args: &mut Vec, ln: bool) { + for arg in args { match arg { - Value::Array(arg) => { - print!("{:#?}", arg); + Value::Array(args) => { + print!("{}", serde_json::to_string_pretty(args).unwrap()); } Value::String(arg) => { print!("{}", arg); } Value::Bool(arg) => { - print!("{}", arg); + print!("{}", arg.to_string().blue()); } Value::Number(arg) => { - print!("{}", arg); + print!("{}", arg.to_string().truecolor(180, 208, 143)); } Value::Object(arg) => { - print!("{:#?}", arg); + print!("{}", serde_json::to_string_pretty(arg).unwrap()); } Value::Null => { - print!("null"); + print!("{}", "null".blue()); } } + if ln == true { + println!(); + } } - println!(""); + if ln == false { + println!(); + } + } + fn exit(&self) { + println!("{}", "Programm finished with exit code: 0".green()); + std::process::exit(0); + } + fn err_exit(&self) { + println!("{}", "Programm finished with exit code: 1".red()); + std::process::exit(1); + } + fn unk_token(&self, name: &str) { + println!( + "{} {} | {} {}", + "Unexpected token name:".red(), + name.bold().black(), + "pos:".green(), + self.pos + ); + std::process::exit(1); + } + + fn error(&self, name: &str) { + println!( + "{} {} | {} {}", + "Error:".red(), + name.bold().black(), + "pos:".green(), + self.pos + ); + std::process::exit(1); } } diff --git a/src/main.rs b/src/main.rs index ecf432b..5e7700b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ use clap::Parser; -use json5; -use serde_json::Value; use std::fs; use std::time::Instant; diff --git a/test.json5 b/test.json5 index f80b445..636668e 100644 --- a/test.json5 +++ b/test.json5 @@ -1,8 +1,21 @@ [ + //print functions { - print: ["Hello world", "! ", 1, null], + print: ["Hello world", "! ", 1, "\n", ["wow"]], }, { - print: ["Khm", "?"], + println: ["Hello", "world", "!"], }, + { + print: "Fool", + }, + "Really?", + + [2, "Yes \n", true], + + true, //throw error + + //Exit functions + "Exit", + "ErrExit", ]