mirror of
https://github.com/artegoser/ONLang
synced 2025-02-23 12:43:13 +03:00
feat: print
This commit is contained in:
commit
f14d3bead2
6 changed files with 549 additions and 0 deletions
57
src/interpreter.rs
Normal file
57
src/interpreter.rs
Normal file
|
@ -0,0 +1,57 @@
|
|||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Interpreter {
|
||||
input: String,
|
||||
vars: HashMap<String, Value>,
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl Interpreter {
|
||||
pub fn new(input: String) -> Interpreter {
|
||||
Interpreter {
|
||||
input,
|
||||
vars: HashMap::new(),
|
||||
pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
let mut obj = json5::from_str::<Value>(&self.input).unwrap();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print(&self, args: &Value) {
|
||||
for arg in args.as_array().unwrap() {
|
||||
match arg {
|
||||
Value::Array(arg) => {
|
||||
print!("{:#?}", arg);
|
||||
}
|
||||
Value::String(arg) => {
|
||||
print!("{}", arg);
|
||||
}
|
||||
Value::Bool(arg) => {
|
||||
print!("{}", arg);
|
||||
}
|
||||
Value::Number(arg) => {
|
||||
print!("{}", arg);
|
||||
}
|
||||
Value::Object(arg) => {
|
||||
print!("{:#?}", arg);
|
||||
}
|
||||
Value::Null => {
|
||||
print!("null");
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
}
|
35
src/main.rs
Normal file
35
src/main.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use clap::Parser;
|
||||
use json5;
|
||||
use serde_json::Value;
|
||||
use std::fs;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
file: String,
|
||||
|
||||
#[clap(short, long)]
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
mod interpreter;
|
||||
use interpreter::Interpreter;
|
||||
|
||||
fn main() {
|
||||
let start = Instant::now();
|
||||
|
||||
let args = Args::parse();
|
||||
if args.verbose == true {
|
||||
println!("Running: {}\n", args.file);
|
||||
}
|
||||
let file_input = fs::read_to_string(args.file).unwrap().parse().unwrap();
|
||||
|
||||
let mut onint = Interpreter::new(file_input);
|
||||
|
||||
onint.run();
|
||||
|
||||
if args.verbose == true {
|
||||
println!("\nElapsed: {:?}", start.elapsed());
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue