Skip to content

Commit

Permalink
feat: input, sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
artegoser committed Aug 4, 2022
1 parent 89207ca commit 10ebab6
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 29 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "onlang"
name = "on"
version = "0.1.0"
edition = "2021"

Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
![onlang logo](static/logos/OnLang-transparent.png)

# You may have questions

ONLang - Object Notation Language (js`ON`)

## 1. God, what the f\*\*\*\* is this

ONLang is an experimental, esoteric programming language, that allows you to use **json** for **PROGRAMMING**. The interpreter currently supports only json5 format.

## 2. What is it for

For writing simple scripts.

## 3. How run scripts

1. Add the executable file to the path variable
2. `on example.json5`

or

1. Clone this repo
2. `cargo run --quiet --release -- example.json5`

## 4. How to write on this

[Documentation](doc/main.md)

If you want to help create a pull request
12 changes: 12 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# TODO CONCEPTS

1. Functions
2. imports
3. scopes of visibility
4. parallel tasks `{parallel:[..tasks]}`
5. types conversion
6. methods for arrays
7. writing objects to a variable
8. yaml support?

and something else
140 changes: 140 additions & 0 deletions doc/main.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# How to

`All posibilities in example.json5`

## How to print

```json5
[
"Just string in array",
["array", "of", "strings"],
{
print: ["Function"],
},
{
println: ["Function"],
},
]
```

## How to calclulate values

works only with numbers (and variables with number type)

```json5
[
{ calc: [2, "*", 3] }, //only 3 arguments
{ calc: [{ var: "some_variable" }, "-", 2] }, //{var:"some_var"} this is a way to get a variable
]
```

### Supported operators

1. \+
2. \-
3. \*
4. \/
5. \%
6. \>\>
7. \<\<
8. \^
9. \&
10. \|

## How to compare values

```json5
[
{ comp: [true, "!=", false] }, //only 3 arguments
{
comp: [
{
comp: [
{ comp: [{ calc: [1, "+", 1] }, ">", 3] },
"==",
{ var: "var_with_bool_value" },
],
},
"&&",
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", true] },
],
}, //more complex comparisons: (( 1 + 1 > 3 ) == var_with_bool_value) && (( 1 + 1 > 3 ) == true)
]
```

### Supported operators for compare

1. ==
2. !=
3. \>
4. \<
5. \>=
6. \<=
7. \&\&
8. \|\|

## How to create a variable

```json5
[
{
let: {
str: "A",
num: 2,
arr: ["Array", "in", "variable"],
},
},

{
let: {
calculated: { calc: [{ var: "num" }, "*", 4] }, //result 8
},
},
]
```

## How to assign variable

```json5
[
{
assign: {
calculated: { calc: [{ var: "calculated" }, "+", 1] }, // calculated = calculated + 1
},
},
]
```

## Loops

```json5
[
{
loop: [
{
if: {
condition: { comp: [{ var: "i" }, ">=", 10] }, //if i >= 10 break loop
body: ["break"],
//else: [..commands] also work
},
},
{ assign: { i: { calc: [{ var: "i" }, "+", 1] } } }, // i += 1
{ print: ["\ri = ", { var: "i" }] },
{ sleep: 500 }, //sleep 500 ms
],
},
]
```

## Input from console

```json5
[
{
let: {
name: { input: "Your name: " },
},
},
{ print: ["Bye, ", { var: "name" }, "!"] },
]
```
51 changes: 33 additions & 18 deletions test.json5 → example.json5
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
["After executing calculated = ", { var: "calculated" }],

"\nRun loop",

{
loop: [
{
Expand All @@ -174,10 +175,11 @@
loop: [
{
if: {
condition: { comp: [{ var: "num" }, ">=", 200000] },
condition: { comp: [{ var: "num" }, ">=", 100000] },
body: ["break"],
},
},

{
assign: {
num: { calc: [{ var: "num" }, "+", 1] },
Expand All @@ -203,7 +205,7 @@
},
{
if: {
condition: { comp: [{ var: "calculated" }, ">=", 200000] },
condition: { comp: [{ var: "calculated" }, ">=", 100000] },
body: ["\n", "break"],
},
},
Expand All @@ -212,30 +214,43 @@

{
if: {
condition: { comp: [{ var: "calculated" }, ">=", 199999] },
body: [["Calculated is >= ", 199999]],
else: [["Calculated is < ", 199999]],
condition: { comp: [{ var: "calculated" }, ">=", 99999] },
body: [["сalculated is >= ", 99999]],
else: [["сalculated is < ", 99999]],
},
},

{
if: {
condition: { comp: [{ var: "calculated" }, ">", 200000] },
body: [["Calculated is > ", 200000]],
else: [["Calculated is <= ", 200000]],
condition: { comp: [{ var: "calculated" }, ">", 100000] },
body: [["сalculated is > ", 100000]],
else: [["сalculated is <= ", 100000]],
},
},

"clear",
{ let: { i: 0 } },

// //TODO CONCEPTS ##########################################################################################################################################################################################################################
"\nLoop with sleep",

// {
// while: {
// cond: { comp: [1, ">=", {var: "variable"}] },
// body: [
// //commands
// ],
// },
// },
{
loop: [
{
if: {
condition: { comp: [{ var: "i" }, ">=", 10] },
body: ["break"],
},
},
{ assign: { i: { calc: [{ var: "i" }, "+", 1] } } },
{ print: ["\ri = ", { var: "i" }] },
{ sleep: 500 },
],
},

"\nInput a var",
{
let: {
name: { input: "Your name: " },
},
},
{ print: ["Bye, ", { var: "name" }, "!"] },
]
36 changes: 34 additions & 2 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use colored::*;
use json5;
use serde_json::{json, Map, Value};
use std::collections::HashMap;
use std::io::{self, Write};
use std::{thread, time};
pub struct Interpreter {
input: String,
vars: HashMap<String, Value>,
Expand All @@ -18,8 +20,8 @@ impl Interpreter {
}

pub fn run(&mut self) {
let obj = json5::from_str::<Value>(&self.input).unwrap();
let arr = obj.as_array().unwrap();
let obj = json5::from_str::<Value>(&self.input).expect("Your json is invalid!");
let arr = obj.as_array().expect("Json must be an array!");

for command in arr {
self.eval_node(command);
Expand Down Expand Up @@ -102,6 +104,22 @@ impl Interpreter {
self.error("Unsupported data type for the let argument");
}
},
"input" => match value {
Value::String(value) => {
return self.input(value);
}
_ => {
self.error("Unsupported data type for the input argument");
}
},
"sleep" => match value {
Value::Number(value) => {
self.sleep(value);
}
_ => {
self.error("Unsupported data type for the sleep argument");
}
},
"if" => match value {
Value::Object(value) => {
return self.if_node(value);
Expand Down Expand Up @@ -150,10 +168,23 @@ impl Interpreter {
return Value::Null;
}

fn sleep(&self, value: &serde_json::Number) {
let value = value.as_f64().unwrap() as u64;
thread::sleep(time::Duration::from_millis(value));
}

fn clear(&self) {
print!("{}[2J", 27 as char);
}

fn input(&self, value: &String) -> Value {
let mut input = String::new();
print!("{}", value);
io::stdout().flush().unwrap_or_default();
io::stdin().read_line(&mut input).unwrap_or_default();
Value::String(input.trim_end().to_string())
}

fn if_node(&mut self, value: &Map<String, Value>) -> Value {
let condition = self.eval_node(&value["condition"]);
let nodes = &value.get("body");
Expand Down Expand Up @@ -477,6 +508,7 @@ impl Interpreter {
if ln == true {
println!();
}
io::stdout().flush().unwrap_or_default();
}

fn print_one(&mut self, arg: &Value) {
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::Parser;
use std::fs;
use std::time::Instant;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
Expand Down

0 comments on commit 10ebab6

Please sign in to comment.