This commit is contained in:
2025-08-11 08:57:45 +10:00
parent f8397e85d4
commit 566d3aa0fb
2 changed files with 123 additions and 6 deletions

View File

@@ -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.
@@ -50,12 +50,14 @@ Reference a list (a list reference) with an asterisk:
setlist *myList $value1 $value2 # and so on setlist *myList $value1 $value2 # and so on
``` ```
### Keywords ## 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.
@@ -74,6 +76,8 @@ Ends the program. Requires an integer for a status code.
Usage: `end $intvalue` Usage: `end $intvalue`
### I/O
#### stdin #### stdin
Allows input from the console. Allows input from the console.
@@ -92,6 +96,8 @@ 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.
@@ -104,7 +110,7 @@ Allows you to initialize a list.
Usage: `setlist *list $value1 $value2 $value3...` Usage: `setlist *list $value1 $value2 $value3...`
#### setlistat (WORK IN PROGRESS) #### setlistat
Sets a list item at an index. The item at the index must already exist. Lists are index 0. Sets a list item at an index. The item at the index must already exist. Lists are index 0.
@@ -122,6 +128,28 @@ Gets the size of a list and puts it in the variable provided.
Usage: `getlistsize *list &var` Usage: `getlistsize *list &var`
#### listappend
Appends an item to a list.
Usage: `listappend *list $var`
### String Operations (ALL WORK IN PROGRESS)
#### 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.
@@ -146,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.
@@ -169,3 +199,55 @@ 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 !functionname -type &var -type &var -type &var # and so on...`
#### 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.
Usage: `call !function`

View File

@@ -20,8 +20,8 @@
With the licence out of the way, let's begin! With the licence out of the way, let's begin!
Ground is a programming language which takes some inspiration from Ground is a programming language which takes some inspiration from
Assembly in it's design, but has higher level features (more types, Assembly in its design, but has higher level features (more types,
simpler IO, easier variables, etc) which make it easy to use. simpler IO, easier variables, etc) which makes it easy to use.
Ground works even better if you write a programming language that Ground works even better if you write a programming language that
compiles to Ground code. Ground is designed to have a similar compiles to Ground code. Ground is designed to have a similar
@@ -54,7 +54,7 @@ enum class Instructions {
Add, Subtract, Multiply, Divide, Add, Subtract, Multiply, Divide,
Equal, Inequal, Greater, Lesser, Equal, Inequal, Greater, Lesser,
End, Set, Empty, End, Set, Empty,
Setlist, Getlistat, Setlistat, Getlistsize Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend
}; };
/* /*
@@ -531,6 +531,10 @@ void exec(vector<Instruction> in) {
} }
} }
break; break;
/*
setlistat instruction
This instruction sets an item in a list to be a certain value.
*/
case Instructions::Setlistat: case Instructions::Setlistat:
if (l.args.size() < 3) { if (l.args.size() < 3) {
error("Could not find all arguments required for Setlistat inbuilt"); error("Could not find all arguments required for Setlistat inbuilt");
@@ -573,6 +577,37 @@ void exec(vector<Instruction> in) {
} }
} }
break; break;
/*
listappend instruction
This instruction appends an item to a list.
*/
case Instructions::Listappend:
if (l.args.size() < 2) {
error("Could not find all arguments required for Listappend inbuilt");
}
{
ListRef listref;
Literal value;
if (holds_alternative<ListRef>(l.args[0])) {
listref = get<ListRef>(l.args[0]);
} else {
error("Second argument of listappend must be a list reference");
}
if (holds_alternative<Literal>(l.args[1])) {
value = get<Literal>(l.args[1]);
} else {
error("Second argument of listappend must be a direct reference");
}
if (lists.find(listref.listName) != lists.end()) {
lists[listref.listName].val.push_back(value);
} else {
error("Unknown list: " + listref.listName);
}
}
break;
/* /*
getlistsize instruction getlistsize instruction
This instruction saves the size of a list in a variable. This instruction saves the size of a list in a variable.