https://github.com/junnishimura/jsop
Let's write programs in JSON!!
https://github.com/junnishimura/jsop
go json programming-language
Last synced: 11 months ago
JSON representation
Let's write programs in JSON!!
- Host: GitHub
- URL: https://github.com/junnishimura/jsop
- Owner: JunNishimura
- License: mit
- Created: 2024-09-18T15:49:44.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-16T16:33:49.000Z (over 1 year ago)
- Last Synced: 2024-10-19T09:06:24.725Z (over 1 year ago)
- Topics: go, json, programming-language
- Language: Go
- Homepage:
- Size: 177 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
JSOP
write program in JSON
```json
{
"command": {
"symbol": "print",
"args": "Hello, World!"
}
}
```
## 💾 Installation
### Homebrew Tap
```
brew install JunNishimura/tap/JSOP
```
### go intall
```
go install github.com/JunNishimura/jsop@latest
```
### GitHub Releases
Download exec files from [GitHub Releases](https://github.com/JunNishimura/JSOP/releases).
## 💾 How to use
Since REPL is not provided, you can write your program in any file and pass the file path as a command line argument to execute the program.
```bash
jsop ./path/to/file.jsop.json
```
## 📖 Language Specification
1. Everything is an expression.
2. Only `.jsop` and `.jsop.json` are accepted as file extensions.
### Integer
Integer value is a sequence of numbers.
Example
```json
123
```
### String
String value is a sequence of letters, symbols, and spaces enclosed in double quotation marks.
Example
```json
"this is a string"
```
### Boolean
Boolean value is either `true` or `false`.
Example
```json
true
```
### Array
Arrays are composed of expressions.
Example
```json
[1, "string", true]
```
### Identifiers
Strings beginning with the `$` symbol are considered as identifiers.
Example
```json
"$x"
```
Embedding the Identifier in a string is accomplished by using curly brackets.
Example
```json
"{$hello}, world!"
```
### Assignment
To assign a value or function to an identifier, use the `set` key.
| parent key | children key | explanation |
| ---- | ---- | ---- |
| set | | declaration of assignment |
| | var | identifier name |
| | val | value to assign |
Example
```json
[
{
"set": {
"var": "$x",
"val": 10
}
},
"$x"
]
```
### Function
#### Function Definition
Functions can be defined by using `set` key and `lambda` expression`.
| parent key | children key | explanation |
| ---- | ---- | ---- |
| lambda | | declaration |
| | params | parameters(optional) |
| | body | body of function |
Example
```json
{
"set": {
"var": "$add",
"val": {
"lambda": {
"params": ["$x", "$y"],
"body": {
"command": {
"symbol": "+",
"args": ["$x", "$y"]
}
}
}
}
}
}
```
#### Function Call
Functions can be called by using `command` key.
| parent key | children key | explanation |
| ---- | ---- | ---- |
| command | | declaration of function calling |
| | symbol | function to call |
| | args | arguments(optional) |
Example
```json
{
"command": {
"symbol": "+",
"args": [1, 2]
}
}
```
### Builtin Functions
Builtin functions are as follows,
| 関数 | explanation |
| ---- | ---- |
| + | addition |
| - | subtraction |
| * | multiplication |
| / | division |
| % | modulo |
| ! | negation |
| && | and operation |
| \|\| | or operation |
| == | equation |
| != | non equation |
| > | greater than |
| >= | greater than equal |
| < | smaller than |
| >= | smaller than equal |
| print | print to standard output |
| len | length of array |
| at | access to the element of array |
### If
Conditional branches can be implemented by using the `if` key.
| parent key | children key | explanation |
| ---- | ---- | ---- |
| if | | declaratoin of if |
| | cond | condition |
| | conseq | consequence(the program to execute when cond is true) |
| | alt | alternative(the program to execute when cond is false) |
Example
```json
{
"if": {
"cond": {
"command": {
"symbol": "==",
"args": [1, 2]
}
},
"conseq": {
"command": {
"symbol": "+",
"args": [3, 4]
}
},
"alt": {
"command": {
"symbol": "*",
"args": [5, 6]
}
}
}
}
```
### Loop
Iterations are handled by using the `loop` key.
| parent key | children key | explanation |
| ---- | ---- | ---- |
| loop | | declaration of loop |
| | for | the identifier for loop counter |
| | from | the initial value of loop counter |
| | until | loop termination condition (break when loop counter equals this value) |
| | do | Iterative processing body |
Example
```json
{
"loop": {
"for": "$i",
"from": 0,
"until": 10,
"do": {
"command": {
"symbol": "print",
"args": "$i"
}
}
}
}
```
You can also perform a loop operation on the elements of an Array. Unlike the example above, the `in` key specifies an Array.
Example
```json
[
{
"set": {
"var": "$arr",
"val": [10, 20, 30]
}
},
{
"loop": {
"for": "$element",
"in": "$arr",
"do": {
"command": {
"symbol": "print",
"args": "$element"
}
}
}
}
]
```
Also, insert `break` and `continue` as keys as follows.
```json
[
{
"set": {
"var": "$sum",
"val": 0
}
},
{
"loop": {
"for": "$i",
"from": 1,
"until": 15,
"do": {
"if": {
"cond": {
"command": {
"symbol": ">",
"args": ["$i", 10]
}
},
"conseq": {
"break": {}
},
"alt": {
"if": {
"cond": {
"command": {
"symbol": "==",
"args": [
{
"command": {
"symbol": "%",
"args": ["$i", 2]
}
},
0
]
}
},
"conseq": {
"set": {
"var": "$sum",
"val": {
"command": {
"symbol": "+",
"args": ["$sum", "$i"]
}
}
}
},
"alt": {
"continue": {}
}
}
}
}
}
}
},
"$sum"
]
```
### Retrun
Use `return` key when you exit the program with return.
```json:return.jsop.json
[
{
"set": {
"var": "$f",
"val": {
"lambda": {
"body": [
{
"set": {
"var": "$sum",
"val": 0
}
},
{
"loop": {
"for": "$i",
"from": 1,
"until": 11,
"do": {
"if": {
"cond": {
"command": {
"symbol": ">",
"args": ["$i", 5]
}
},
"conseq": {
"return": "$sum"
},
"alt": {
"set": {
"var": "$sum",
"val": {
"command": {
"symbol": "+",
"args": ["$sum", "$i"]
}
}
}
}
}
}
}
}
]
}
}
}
},
{
"command": {
"symbol": "$f"
}
}
]
```
### Macro
Macro can be defined by using `defmacro` key.
| parent key | children key | explanation |
| ---- | ---- | ---- |
| defmacro | | declaration of macro definition |
| | name | name of macro |
| | keys | keys |
| | body | the body of macro |
You can also call the `quote` symbol for quoting, and unquote by adding backquotes to the beginning of the string.
Example
```json
{
"defmacro": {
"name": "unless",
"keys": ["cond", "conseq", "alt"],
"body": {
"command": {
"symbol": "quote",
"args": {
"if": {
"cond": {
"command": {
"symbol": "!",
"args": ",cond"
}
},
"conseq": ",conseq",
"alt": ",alt"
}
}
}
}
}
}
```
Defining function can be much simpler if you use Macro.
Example
```json
[
{
"defmacro": {
"name": "defun",
"keys": ["name", "params", "body"],
"body": {
"command": {
"symbol": "quote",
"args": {
"set": {
"var": ",name",
"val": {
"lambda": {
"params": ",params",
"body": ",body"
}
}
}
}
}
}
}
},
{
"defun": {
"name": "$add",
"params": ["$x", "$y"],
"body": {
"command": {
"symbol": "+",
"args": ["$x", "$y"]
}
}
}
},
{
"command": {
"symbol": "$add",
"args": [1, 2]
}
}
]
```
### Comment
Comments can be inesrted by using `//` key.
Example
```json
{
"//": "this is a function to add two values",
"set": {
"var": "$add",
"val": {
"lambda": {
"params": ["$x", "$y"],
"body": {
"command": {
"symbol": "+",
"args": ["$x", "$y"]
}
}
}
}
}
}
```
## 🤔 FizzBuzz Problem
Finally, I have included an example of solving a FizzBuzz problem in JSOP.
Example
```json
[
{
"set": {
"var": "$num",
"val": 31
}
},
{
"loop": {
"for": "$i",
"from": 1,
"until": "$num",
"do": {
"if": {
"cond": {
"command": {
"symbol": "&&",
"args": [
{
"command": {
"symbol": "==",
"args": [
{
"command": {
"symbol": "%",
"args": ["$i", 3]
}
},
0
]
}
},
{
"command": {
"symbol": "==",
"args": [
{
"command": {
"symbol": "%",
"args": ["$i", 5]
}
},
0
]
}
}
]
}
},
"conseq": {
"command": {
"symbol": "print",
"args": "{$i}: FizzBuzz"
}
},
"alt": {
"if": {
"cond": {
"command": {
"symbol": "==",
"args": [
{
"command": {
"symbol": "%",
"args": ["$i", 3]
}
},
0
]
}
},
"conseq": {
"command": {
"symbol": "print",
"args": "{$i}: Fizz"
}
},
"alt": {
"if": {
"cond": {
"command": {
"symbol": "==",
"args": [
{
"command": {
"symbol": "%",
"args": ["$i", 5]
}
},
0
]
}
},
"conseq": {
"command": {
"symbol": "print",
"args": "{$i}: Buzz"
}
},
"alt": {
"command": {
"symbol": "print",
"args": "$i"
}
}
}
}
}
}
}
}
}
}
]
```
## 🪧 License
JSOP is released under MIT License. See [MIT](https://raw.githubusercontent.com/JunNishimura/JSOP/main/LICENSE)