16 Commits

Author SHA1 Message Date
b19b4123d8 Parser string and character fix 2025-08-18 13:38:26 +10:00
28a9e389fa Basic function calling support 2025-08-18 09:36:35 +10:00
e4cc6b2f14 Fix critical bug, further functions 2025-08-15 11:35:58 +10:00
bb753e97d4 Return function 2025-08-13 18:31:54 +10:00
4cd4d9080d Function declarations 2025-08-13 09:40:03 +10:00
52eadaa9c3 Typerefs and functionrefs 2025-08-12 09:45:00 +10:00
db0c362efb Start work on functions 2025-08-11 14:57:45 +10:00
3a8600b481 Merge branch 'master' of https://git.maxwellj.xyz/max/ground 2025-08-11 14:13:00 +10:00
c39967a72f Type conversion 2025-08-11 14:12:25 +10:00
163f85b896 Fix a typo 2025-08-11 13:29:10 +10:00
09033cd432 More stuff 2025-08-11 10:07:05 +10:00
566d3aa0fb updates 2025-08-11 08:57:45 +10:00
f8397e85d4 Labels 2025-08-10 16:08:56 +10:00
3f2482d7ea Labels 2025-08-10 15:42:52 +10:00
2e388c6e68 Start work on lists 2025-08-10 13:31:28 +10:00
f43f79b869 Bugfix: no longer treats floats as ints 2025-08-09 19:52:49 +10:00
10 changed files with 1079 additions and 44 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
ground
Bobfile

View File

@@ -1,5 +1,5 @@
compiler "g++";
binary "ground";
source "src/main.cpp";
flag "O3";
flag "Ofast";
compile;

View File

@@ -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.
* **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.
## 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:
```
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.)

View File

@@ -1,6 +1,6 @@
## Ground Syntax Guide
### General syntax
## General syntax
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
```
### 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: 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
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.
Usage: `end $value`
Usage: `end $intvalue`
### I/O
#### stdin
@@ -74,12 +96,60 @@ Allows output to the console, appending a new line at the end.
Usage: `stdlnout $value`
### Variables and Lists
#### set
Allows you to set a variable to a 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
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`
### Comparisons
#### equal
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.
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`

File diff suppressed because it is too large Load Diff

89
tests/everything.grnd Normal file
View 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
View 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

View File

@@ -1,7 +1,9 @@
@begin
stdout "Do you like cheese? "
stdin &userin
equal $userin "yes" &condition
if $condition %7
if $condition %end
stdlnout "That is sad"
jump %1
jump %begin
@end
stdlnout "Awesome! I do too!"

13
tests/lists.grnd Normal file
View 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
View 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