Compare commits
16 Commits
0.0.1
...
b19b4123d8
| Author | SHA1 | Date | |
|---|---|---|---|
| b19b4123d8 | |||
| 28a9e389fa | |||
| e4cc6b2f14 | |||
| bb753e97d4 | |||
| 4cd4d9080d | |||
| 52eadaa9c3 | |||
| db0c362efb | |||
| 3a8600b481 | |||
| c39967a72f | |||
| 163f85b896 | |||
| 09033cd432 | |||
| 566d3aa0fb | |||
| f8397e85d4 | |||
| 3f2482d7ea | |||
| 2e388c6e68 | |||
| f43f79b869 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
ground
|
ground
|
||||||
|
Bobfile
|
||||||
|
|||||||
2
Bobfile
2
Bobfile
@@ -1,5 +1,5 @@
|
|||||||
compiler "g++";
|
compiler "g++";
|
||||||
binary "ground";
|
binary "ground";
|
||||||
source "src/main.cpp";
|
source "src/main.cpp";
|
||||||
flag "O3";
|
flag "Ofast";
|
||||||
compile;
|
compile;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ Ground is an interpreter which processes and interprets Ground instructions. It
|
|||||||
|
|
||||||
* **Simple syntax:** Ground doesn't have very many features, and that's intentional. It makes Ground easy to learn, and keeps it speedy.
|
* **Simple syntax:** Ground doesn't have very many features, and that's intentional. It makes Ground easy to learn, and keeps it speedy.
|
||||||
* **Super speed:** Ground code is faster than Python and JavaScript, and nearly as fast as C++ and Rust, while still being interpreted. (Tested using tests/to1000.grnd)
|
* **Super speed:** Ground code is faster than Python and JavaScript, and nearly as fast as C++ and Rust, while still being interpreted. (Tested using tests/to1000.grnd)
|
||||||
* **Tiny interpreter:** Ground contains 761 lines of code (and 233 lines of comments) at the time of writing, and compiles in seconds.
|
* **Tiny interpreter:** Ground contains 1154 lines of code (and 320 lines of comments) at the time of writing, and compiles in seconds.
|
||||||
* **Portable:** Ground's code only uses features from the C++ standard library, using features from C++17 and prior.
|
* **Portable:** Ground's code only uses features from the C++ standard library, using features from C++17 and prior.
|
||||||
|
|
||||||
## How do I get started?
|
## How do I get started?
|
||||||
@@ -16,7 +16,7 @@ Ground is an interpreter which processes and interprets Ground instructions. It
|
|||||||
Clone the repo and compile with your favourite C++ compiler:
|
Clone the repo and compile with your favourite C++ compiler:
|
||||||
|
|
||||||
```
|
```
|
||||||
g++ src/main.cpp -std=C++17 -O3 -o ground
|
g++ src/main.cpp -std=c++17 -O3 -o ground
|
||||||
```
|
```
|
||||||
|
|
||||||
(You can omit the -std flag on systems which default to the latest standard, and the -O3 flag if you're fine with a slightly slower interpreter.)
|
(You can omit the -std flag on systems which default to the latest standard, and the -O3 flag if you're fine with a slightly slower interpreter.)
|
||||||
|
|||||||
158
docs/syntax.md
158
docs/syntax.md
@@ -1,6 +1,6 @@
|
|||||||
## Ground Syntax Guide
|
## Ground Syntax Guide
|
||||||
|
|
||||||
### General syntax
|
## General syntax
|
||||||
|
|
||||||
Ground uses simple instructions and arguments to run code.
|
Ground uses simple instructions and arguments to run code.
|
||||||
|
|
||||||
@@ -32,12 +32,32 @@ Reference a line (a line reference) with a percent symbol before a line number:
|
|||||||
jump %10
|
jump %10
|
||||||
```
|
```
|
||||||
|
|
||||||
### Keywords
|
Alternatively, set a label:
|
||||||
|
|
||||||
|
```
|
||||||
|
@myLabel # The '@' symbol denotes setting a label
|
||||||
|
```
|
||||||
|
|
||||||
|
and jump to that (setting labels will be discussed below):
|
||||||
|
|
||||||
|
```
|
||||||
|
jump %myLabel
|
||||||
|
```
|
||||||
|
|
||||||
|
Reference a list (a list reference) with an asterisk:
|
||||||
|
|
||||||
|
```
|
||||||
|
setlist *myList $value1 $value2 # and so on
|
||||||
|
```
|
||||||
|
|
||||||
|
## Keywords
|
||||||
|
|
||||||
Note: &var can be replaced with any direct reference. $value can be replaced with a literal value or a value reference. %1 can be replaced with a line reference.
|
Note: &var can be replaced with any direct reference. $value can be replaced with a literal value or a value reference. %1 can be replaced with a line reference.
|
||||||
|
|
||||||
Note: In most of these functions, if a direct reference is used, the value outputted by that function will be avaliable at that variable. Any existing value inside that variable will be overwritten.
|
Note: In most of these functions, if a direct reference is used, the value outputted by that function will be avaliable at that variable. Any existing value inside that variable will be overwritten.
|
||||||
|
|
||||||
|
### Control Flow
|
||||||
|
|
||||||
#### if
|
#### if
|
||||||
|
|
||||||
Make a decision based on a boolean. If the boolean is true, jumps to the line referenced.
|
Make a decision based on a boolean. If the boolean is true, jumps to the line referenced.
|
||||||
@@ -54,7 +74,9 @@ Usage: `jump %1`
|
|||||||
|
|
||||||
Ends the program. Requires an integer for a status code.
|
Ends the program. Requires an integer for a status code.
|
||||||
|
|
||||||
Usage: `end $value`
|
Usage: `end $intvalue`
|
||||||
|
|
||||||
|
### I/O
|
||||||
|
|
||||||
#### stdin
|
#### stdin
|
||||||
|
|
||||||
@@ -74,12 +96,60 @@ Allows output to the console, appending a new line at the end.
|
|||||||
|
|
||||||
Usage: `stdlnout $value`
|
Usage: `stdlnout $value`
|
||||||
|
|
||||||
|
### Variables and Lists
|
||||||
|
|
||||||
#### set
|
#### set
|
||||||
|
|
||||||
Allows you to set a variable to a value.
|
Allows you to set a variable to a value.
|
||||||
|
|
||||||
Usage: `set &var $value`
|
Usage: `set &var $value`
|
||||||
|
|
||||||
|
#### setlist
|
||||||
|
|
||||||
|
Allows you to initialize a list.
|
||||||
|
|
||||||
|
Usage: `setlist *list $value1 $value2 $value3...`
|
||||||
|
|
||||||
|
#### setlistat
|
||||||
|
|
||||||
|
Sets a list item at an index. The item at the index must already exist. Lists are index 0.
|
||||||
|
|
||||||
|
Usage: `setlistat *list $intvalue $value`
|
||||||
|
|
||||||
|
#### getlistat
|
||||||
|
|
||||||
|
Gets a list item at an index, and puts it in the variable provided. The item at the index must already exist. Lists are index 0.
|
||||||
|
|
||||||
|
Usage: `getlistat *list $intvalue &var`
|
||||||
|
|
||||||
|
#### getlistsize
|
||||||
|
|
||||||
|
Gets the size of a list and puts it in the variable provided.
|
||||||
|
|
||||||
|
Usage: `getlistsize *list &var`
|
||||||
|
|
||||||
|
#### listappend
|
||||||
|
|
||||||
|
Appends an item to a list.
|
||||||
|
|
||||||
|
Usage: `listappend *list $var`
|
||||||
|
|
||||||
|
### String Operations
|
||||||
|
|
||||||
|
#### getstrsize
|
||||||
|
|
||||||
|
Gets the size of a string and puts it in the variable provided.
|
||||||
|
|
||||||
|
Usage: `getstrsize $stringvalue &var`
|
||||||
|
|
||||||
|
#### getstrcharat
|
||||||
|
|
||||||
|
Gets a character at a certain position in a string and saves it to a variable.
|
||||||
|
|
||||||
|
Usage: `getstrcharat $stringvalue $intvalue &var`
|
||||||
|
|
||||||
|
### Maths
|
||||||
|
|
||||||
#### add
|
#### add
|
||||||
|
|
||||||
Adds two numbers. Numbers mean an integer or a double. Outputs to a direct reference.
|
Adds two numbers. Numbers mean an integer or a double. Outputs to a direct reference.
|
||||||
@@ -104,6 +174,8 @@ Divides two numbers. Numbers mean an integer or a double. Outputs to a direct re
|
|||||||
|
|
||||||
Usage: `divide $value $value &var`
|
Usage: `divide $value $value &var`
|
||||||
|
|
||||||
|
### Comparisons
|
||||||
|
|
||||||
#### equal
|
#### equal
|
||||||
|
|
||||||
Checks if two values are equal. Outputs a boolean to a direct reference.
|
Checks if two values are equal. Outputs a boolean to a direct reference.
|
||||||
@@ -126,4 +198,82 @@ Usage: `greater $value $value &var`
|
|||||||
|
|
||||||
Checks if the left value is lesser than the right value. Outputs a boolean to a direct reference.
|
Checks if the left value is lesser than the right value. Outputs a boolean to a direct reference.
|
||||||
|
|
||||||
Usage: `lesser $value $value &var`
|
Usage: `lesser $value $value &var`
|
||||||
|
|
||||||
|
### Type Conversions
|
||||||
|
|
||||||
|
#### stoi
|
||||||
|
|
||||||
|
Converts a string to an integer. Throws an error if the string cannot be turned into an integer.
|
||||||
|
|
||||||
|
Usage: `stoi $stringvalue &var`
|
||||||
|
|
||||||
|
#### stod
|
||||||
|
|
||||||
|
Converts a string to a double. Throws an error if the string cannot be turned into a double.
|
||||||
|
|
||||||
|
Usage: `stod $stringvalue &var`
|
||||||
|
|
||||||
|
#### tostring
|
||||||
|
|
||||||
|
Converts any type to a string.
|
||||||
|
|
||||||
|
Usage: `tostring $value &var`
|
||||||
|
|
||||||
|
### Functions and function specific features (ALL WORK IN PROGRESS)
|
||||||
|
|
||||||
|
Some symbols specific to this category:
|
||||||
|
|
||||||
|
* `!function`: A function reference
|
||||||
|
|
||||||
|
* `-type`: A type reference. Can be one of the following: "-string", "-char", "-int", "-double", "-bool"
|
||||||
|
|
||||||
|
#### fun
|
||||||
|
|
||||||
|
Defines a function. All code between `fun` and `endfun` will be included in the function.
|
||||||
|
|
||||||
|
Usage: `fun -type !functionname -type &var -type &var -type &var # and so on...`
|
||||||
|
|
||||||
|
Usage note: The first type specified before the function name must be the return type. The type displayed before all vars shows what type that variable must be.
|
||||||
|
|
||||||
|
#### return
|
||||||
|
|
||||||
|
Returns back to the main program (or other function that called this function). Also returns a value, which must be of the type defined when defining the function.
|
||||||
|
|
||||||
|
Usage: `return $value`
|
||||||
|
|
||||||
|
#### endfun
|
||||||
|
|
||||||
|
Ends a function definition. When a function reaches the end the argument list will be cleared.
|
||||||
|
|
||||||
|
Usage: `endfun`
|
||||||
|
|
||||||
|
#### pusharg
|
||||||
|
|
||||||
|
Adds a value to the argument list which will be passed to the function when it is called.
|
||||||
|
|
||||||
|
Usage: `pusharg $value`
|
||||||
|
|
||||||
|
#### call
|
||||||
|
|
||||||
|
Calls a function, with all the arguments in the argument list. The return value will be put in the specified variable.
|
||||||
|
|
||||||
|
Usage: `call !function &var
|
||||||
|
|
||||||
|
### Interacting with Libraries (ALL WORK IN PROGRESS)
|
||||||
|
|
||||||
|
#### use
|
||||||
|
|
||||||
|
Attempts to import another Ground program. Gets inserted wherever the use statement is. Any code (including code outside function declarations) will be executed.
|
||||||
|
|
||||||
|
Note: Ground will check the directory where the program is stored when trying to find imported programs. If that fails, it will check the directory set in the $GROUND_PATH environment variable set by your system. The '.grnd' extension is appended automatically.
|
||||||
|
|
||||||
|
Usage: `use $stringvalue`
|
||||||
|
|
||||||
|
#### extern
|
||||||
|
|
||||||
|
Attempts to import a shared object library written for Ground. All functions in the external library will be usable with `call`.
|
||||||
|
|
||||||
|
Note: Ground will check the directory where the program is stored when trying to find external programs. If that fails, it will check the directory set in the $GROUND_PATH environment variable set by your system. The '.so', '.dll', etc extension is appended automatically.
|
||||||
|
|
||||||
|
Usage: `extern $stringvalue`
|
||||||
830
src/main.cpp
830
src/main.cpp
File diff suppressed because it is too large
Load Diff
89
tests/everything.grnd
Normal file
89
tests/everything.grnd
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
# I/O
|
||||||
|
|
||||||
|
stdlnout "Hello there!"
|
||||||
|
stdout "What is your name? "
|
||||||
|
stdin &name
|
||||||
|
add "Hello, " $name &out
|
||||||
|
stdlnout $out
|
||||||
|
|
||||||
|
# Types
|
||||||
|
stdlnout "dingus"
|
||||||
|
stdlnout 7
|
||||||
|
stdlnout 3.14159
|
||||||
|
stdlnout true
|
||||||
|
stdlnout 'e'
|
||||||
|
|
||||||
|
# Variables
|
||||||
|
|
||||||
|
set &testVar "This is a test"
|
||||||
|
stdlnout $testVar
|
||||||
|
|
||||||
|
# Lists
|
||||||
|
|
||||||
|
setlist *testList "Item 1" "Another Item" "Item the Third"
|
||||||
|
getlistat *testList 2 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
setlistat *testList 1 "I changed this item"
|
||||||
|
getlistat *testList 1 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
listappend *testList "I appended this item"
|
||||||
|
getlistat *testList 3 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
getlistsize *testList &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
# String Operations
|
||||||
|
|
||||||
|
set &testStr "dingus"
|
||||||
|
|
||||||
|
getstrsize $testStr &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
getstrcharat $testStr 3 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
# Maths
|
||||||
|
|
||||||
|
add 1 1 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
subtract 10 5 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
multiply 15 15 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
divide 36 4 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
# Comparisons
|
||||||
|
|
||||||
|
equal 5 5 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
inequal 5 5 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
greater 10 5 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
lesser 10 5 &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
|
||||||
|
# Control flow
|
||||||
|
|
||||||
|
set &counter 0
|
||||||
|
|
||||||
|
@myLabel
|
||||||
|
add $counter 1 &counter
|
||||||
|
stdlnout $counter
|
||||||
|
inequal $counter 10 &case
|
||||||
|
if $case %myLabel
|
||||||
|
|
||||||
|
# That's it!
|
||||||
|
|
||||||
|
stdlnout "Everything ran! Check the output to see if it is what is expected."
|
||||||
|
end 0
|
||||||
10
tests/functions.grnd
Normal file
10
tests/functions.grnd
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
fun -int !dingle -string &silly
|
||||||
|
stdlnout "This is inside the function"
|
||||||
|
return 10
|
||||||
|
endfun
|
||||||
|
|
||||||
|
stdlnout "This is outside the function"
|
||||||
|
|
||||||
|
call !dingle &var
|
||||||
|
|
||||||
|
stdlnout $var
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
|
@begin
|
||||||
stdout "Do you like cheese? "
|
stdout "Do you like cheese? "
|
||||||
stdin &userin
|
stdin &userin
|
||||||
equal $userin "yes" &condition
|
equal $userin "yes" &condition
|
||||||
if $condition %7
|
if $condition %end
|
||||||
stdlnout "That is sad"
|
stdlnout "That is sad"
|
||||||
jump %1
|
jump %begin
|
||||||
|
@end
|
||||||
stdlnout "Awesome! I do too!"
|
stdlnout "Awesome! I do too!"
|
||||||
|
|||||||
13
tests/lists.grnd
Normal file
13
tests/lists.grnd
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# A cool list
|
||||||
|
setlist *favWords "hello" "there" "general" "kenobi"
|
||||||
|
|
||||||
|
set &count 0
|
||||||
|
set &passedThrough true
|
||||||
|
|
||||||
|
@jmpbck
|
||||||
|
getlistat *favWords $count &tmp
|
||||||
|
stdlnout $tmp
|
||||||
|
add $count 1 &count
|
||||||
|
getlistsize *favWords &tmp2
|
||||||
|
inequal $count $tmp2 &tmp3
|
||||||
|
if $tmp3 %jmpbck
|
||||||
10
tests/typeconvs.grnd
Normal file
10
tests/typeconvs.grnd
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
stod "3.14" &out
|
||||||
|
stdlnout $out
|
||||||
|
|
||||||
|
stoi "732" &out
|
||||||
|
add 1 $out &out
|
||||||
|
stdlnout $out
|
||||||
|
|
||||||
|
tostring 3.14 &out
|
||||||
|
add $out " is a number of sorts" &out
|
||||||
|
stdlnout $out
|
||||||
Reference in New Issue
Block a user