mirror of
https://github.com/artegoser/ONLang
synced 2024-12-23 09:33:44 +03:00
feat: print style, println, Exit, ErrExit, Errors
This commit is contained in:
parent
f14d3bead2
commit
4cd76f6f82
5 changed files with 142 additions and 31 deletions
38
Cargo.lock
generated
38
Cargo.lock
generated
|
@ -2,16 +2,6 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ONLang"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"clap",
|
|
||||||
"json5",
|
|
||||||
"serde",
|
|
||||||
"serde_json",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "atty"
|
name = "atty"
|
||||||
version = "0.2.14"
|
version = "0.2.14"
|
||||||
|
@ -89,6 +79,17 @@ dependencies = [
|
||||||
"os_str_bytes",
|
"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]]
|
[[package]]
|
||||||
name = "cpufeatures"
|
name = "cpufeatures"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
|
@ -176,6 +177,12 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.126"
|
version = "0.2.126"
|
||||||
|
@ -188,6 +195,17 @@ version = "1.13.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
|
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "onlang"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"clap",
|
||||||
|
"colored",
|
||||||
|
"json5",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "os_str_bytes"
|
name = "os_str_bytes"
|
||||||
version = "6.2.0"
|
version = "6.2.0"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ONLang"
|
name = "onlang"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
@ -10,3 +10,4 @@ serde = { version = "1.0", features = ["derive"] }
|
||||||
json5 = "0.4.1"
|
json5 = "0.4.1"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
clap = { version = "3.2", features = ["derive"] }
|
clap = { version = "3.2", features = ["derive"] }
|
||||||
|
colored = "2"
|
|
@ -1,6 +1,7 @@
|
||||||
|
use colored::*;
|
||||||
|
use json5;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub struct Interpreter {
|
pub struct Interpreter {
|
||||||
input: String,
|
input: String,
|
||||||
vars: HashMap<String, Value>,
|
vars: HashMap<String, Value>,
|
||||||
|
@ -12,7 +13,7 @@ impl Interpreter {
|
||||||
Interpreter {
|
Interpreter {
|
||||||
input,
|
input,
|
||||||
vars: HashMap::new(),
|
vars: HashMap::new(),
|
||||||
pos: 0,
|
pos: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,37 +22,117 @@ impl Interpreter {
|
||||||
let arr = obj.as_array_mut().unwrap();
|
let arr = obj.as_array_mut().unwrap();
|
||||||
|
|
||||||
for command in arr {
|
for command in arr {
|
||||||
for (name, value) in command.as_object().unwrap() {
|
match command {
|
||||||
if name == "print" {
|
Value::Object(command) => {
|
||||||
self.print(value);
|
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: &mut Vec<Value>, ln: bool) {
|
||||||
fn print(&self, args: &Value) {
|
for arg in args {
|
||||||
for arg in args.as_array().unwrap() {
|
|
||||||
match arg {
|
match arg {
|
||||||
Value::Array(arg) => {
|
Value::Array(args) => {
|
||||||
print!("{:#?}", arg);
|
print!("{}", serde_json::to_string_pretty(args).unwrap());
|
||||||
}
|
}
|
||||||
Value::String(arg) => {
|
Value::String(arg) => {
|
||||||
print!("{}", arg);
|
print!("{}", arg);
|
||||||
}
|
}
|
||||||
Value::Bool(arg) => {
|
Value::Bool(arg) => {
|
||||||
print!("{}", arg);
|
print!("{}", arg.to_string().blue());
|
||||||
}
|
}
|
||||||
Value::Number(arg) => {
|
Value::Number(arg) => {
|
||||||
print!("{}", arg);
|
print!("{}", arg.to_string().truecolor(180, 208, 143));
|
||||||
}
|
}
|
||||||
Value::Object(arg) => {
|
Value::Object(arg) => {
|
||||||
print!("{:#?}", arg);
|
print!("{}", serde_json::to_string_pretty(arg).unwrap());
|
||||||
}
|
}
|
||||||
Value::Null => {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use json5;
|
|
||||||
use serde_json::Value;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
|
17
test.json5
17
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",
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Reference in a new issue