started writing lexer after making string lib

This commit is contained in:
2026-04-13 19:59:48 +10:00
parent f67c045845
commit c0c35e4d17
24 changed files with 601 additions and 7 deletions

View File

@@ -1,16 +1,97 @@
use collections use collections
use string
//
// Token type enum.
//
// Each token can be one of these types.
//
enum SolsToken {
Identifier, Literal, Type,
Dot, OpenCurly, CloseCurly, OpenParen, CloseParen, Comma,
OpAdd, OpSub, OpMul, OpDiv, OpAddTo, OpSubTo, OpMulTo, OpDivTo, OpIncrement, OpDecrement, OpSet,
OpGreater, OpLesser, OpEqual, OpInequal, OpEqGreater, OpEqLesser,
KwDef, KwLambda, KwReturn, KwUse, KwStruct, KwEnum, KwConstructor,
KwDestructor, KwDuplicator, KwPrivate, KwProtected, KwPuts, KwIf,
KwWhile, KwNew, KwGround,
LineEnd,
}
//
// Token type map.
//
// If a token matches one of the strings in this hashmap,
// its type gets set to the token type for that string.
//
KEYWORDS = Hashmap()
KEYWORDS.set("puts", SolsToken.KwPuts)
KEYWORDS.set("if", SolsToken.KwIf)
KEYWORDS.set("while", SolsToken.KwWhile)
KEYWORDS.set("def", SolsToken.KwDef)
KEYWORDS.set("lambda", SolsToken.KwLambda)
KEYWORDS.set("return", SolsToken.KwReturn)
KEYWORDS.set("use", SolsToken.KwUse)
KEYWORDS.set("struct", SolsToken.KwStruct)
KEYWORDS.set("enum", SolsToken.KwEnum)
KEYWORDS.set("constructor", SolsToken.KwConstructor)
KEYWORDS.set("destructor", SolsToken.KwDestructor)
KEYWORDS.set("duplicator", SolsToken.KwDuplicator)
KEYWORDS.set("private", SolsToken.KwPrivate)
KEYWORDS.set("protected", SolsToken.KwProtected)
KEYWORDS.set("ground", SolsToken.KwGround)
KEYWORDS.set("new", SolsToken.KwNew)
KEYWORDS.set("{", SolsToken.OpenCurly)
KEYWORDS.set("}", SolsToken.CloseCurly)
KEYWORDS.set("(", SolsToken.OpenParen)
KEYWORDS.set(")", SolsToken.CloseParen)
KEYWORDS.set("+", SolsToken.OpAdd)
KEYWORDS.set("-", SolsToken.OpSub)
KEYWORDS.set("*", SolsToken.OpMul)
KEYWORDS.set("/", SolsToken.OpDiv)
KEYWORDS.set("=", SolsToken.OpSet)
KEYWORDS.set("+=", SolsToken.OpAddTo)
KEYWORDS.set("-=", SolsToken.OpSubTo)
KEYWORDS.set("*=", SolsToken.OpMulTo)
KEYWORDS.set("/=", SolsToken.OpDivTo)
KEYWORDS.set("++", SolsToken.OpIncrement)
KEYWORDS.set("--", SolsToken.OpDecrement)
KEYWORDS.set("==", SolsToken.OpEqual)
KEYWORDS.set("!=", SolsToken.OpInequal)
KEYWORDS.set(">", SolsToken.OpGreater)
KEYWORDS.set("<", SolsToken.OpLesser)
KEYWORDS.set(">=", SolsToken.OpEqGreater)
KEYWORDS.set("<=", SolsToken.OpEqLesser)
KEYWORDS.set("\n", SolsToken.LineEnd)
KEYWORDS.set(";", SolsToken.LineEnd)
KEYWORDS.set(",", SolsToken.Comma)
struct SolsLexer { struct SolsLexer {
input = "" sourceCode = "x = 123"
current = 0 current = 0
constructor(string input) { private def getTokenType(string input) SolsToken {
self.input = input return KEYWORDS.get(input)
} }
def lex() List { def lex() {
output = List(1) inString = false
return output lineNum = 1
lineStart = 0
currentLine = ""
while lineStart < string_Length() {
}
} }
} }
lexer = new SolsLexer
puts lexer.getTokenType("if")

37
string/SUMMARY.md Normal file
View File

@@ -0,0 +1,37 @@
# string
The string library gives you access to a lot of functions that are necessary for doing anything useful with strings.
**Note:** The string in Ground library is called "strings" in Solstice.
## Functions
- [string_CharAt](docs/char_at.md)
### Modifying Strings
- [string_Upper](docs/modify/upper.md)
- [string_Lower](docs/modify/lower.md)
- [string_Trim](docs/modify/trim.md)
- [string_TrimLeft](docs/modify/trim_left.md)
- [string_TrimRight](docs/modify/trim_right.md)
- [string_Substring](docs/modify/substring.md)
- [string_Repeat](docs/modify/repeat.md)
- [string_Replace](docs/modify/replace.md)
- [string_Reverse](docs/modify/reverse.md)
### Check For Substring
- [string_StartsWith](docs/check/starts_with.md)
- [string_EndsWith](docs/check/ends_with.md)
- [string_Contains](docs/check/contains.md)
### Find Substring
- [string_Find](docs/find/find.md)
- [string_FindLast](docs/find/find_last.md)
- [string_Count](docs/find/count.md)
### String Classification
- [string_IsAlpha](docs/classify/is_alpha.md)
- [string_IsAlnum](docs/classify/is_alnum.md)
- [string_IsDigit](docs/classify/is_digit.md)
- [string_IsSpace](docs/classify/is_space.md)
## Changelog
### v1.0.0 (latest)
- Initial release.

24
string/docs/char_at.md Normal file
View File

@@ -0,0 +1,24 @@
# string_CharAt
Get character at index in string.
## Arguments
- str (string): string to get character of.
- index (int): the index in the string you want to get.
## Returns
char (string): the character at the specified index in the string.
## Raises
- `OutOfBounds`: raised if you try accessing a string past its length or at a negative index.
## Example
### Ground
```python
call !string_CharAt "my string" 3 &char
println $char # "s"
```
### Solstice
```c
puts string_CharAt("my string", 3) // "s"
```

View File

@@ -0,0 +1,25 @@
# string_Contains
Check if a string has another string in it.
## Arguments
- haystack (string): the string to check against.
- needle (string): the string to look for.
## Returns
found (bool): returns `true` if `haystack` has `needle` inside it.
## Example
### Ground
```python
call !string_Contains "The task was completed successfully." "success" &found
println $found # true
call !string_Contains "The task failed." "success" &found
println $found # false
```
### Solstice
```c
puts string_Contains("The task was completed successfully.", "success") // true
puts string_Contains("The task failed.", "success") // false
```

View File

@@ -0,0 +1,25 @@
# string_EndsWith
Check if a string has another string as a suffix.
## Arguments
- str (string): the string to check against.
- suffix (string): the suffix to check for.
## Returns
hasSuffix (bool): returns `true` if `str` has the given `suffix` as a suffix, otherwise `false`.
## Example
### Ground
```python
call !string_EndsWith "file.txt" ".txt" &hasSuffix
println $hasSuffix # true
call !string_EndsWith "file.grnd" ".txt" &hasSuffix
println $hasSuffix # false
```
### Solstice
```c
puts string_EndsWith("file.txt", ".txt") // true
puts string_EndsWith("file.grnd", ".txt") // false
```

View File

@@ -0,0 +1,25 @@
# string_StartsWith
Check if a string is prefixed by another string.
## Arguments
- str (string): the string to check against.
- prefix (string): the prefix to check for.
## Returns
hasPrefix (bool): returns `true` if `str` is prefixed by the given `prefix`, otherwise `false`.
## Example
### Ground
```python
call !string_StartsWith "doc_someFile.txt" "doc_" &hasPrefix
println $hasPrefix # true
call !string_StartsWith "someFile.txt" "doc_" &hasPrefix
println $hasPrefix # false
```
### Solstice
```c
puts string_StartsWith("doc_someFile.txt", "doc_") // true
puts string_StartsWith("someFile.txt", "doc_") // false
```

View File

@@ -0,0 +1,24 @@
# string_IsAlnum
Returns true if the given `string` is alphanumeric.
## Arguments
- str (string): the string to check.
## Returns
isAlnum (bool): returns `true` if `string` is alphanumeric, otherwise `false`.
## Example
### Ground
```python
call !string_IsAlnum "abc123" &isAlnum
println $isAlnum # true
call !string_IsAlnum "@$!ffasdf" &isAlnum
println $isAlnum # false
call !string_IsAlnum "1234" &isAlnum
println $isAlnum # true
call !string_IsAlnum "_" &isAlnum
println $isAlnum # false
```

View File

@@ -0,0 +1,24 @@
# string_IsAlpha
Returns true if the given `string` is only the letters a-Z.
## Arguments
- str (string): the string to check.
## Returns
isAlpha (bool): returns `true` if `string` is only the letters a-Z, otherwise `false`.
## Example
### Ground
```python
call !string_IsAlpha "abcABC" &isAlpha
println $isAlpha # true
call !string_IsAlpha "123" &isAlpha
println $isAlpha # false
call !string_IsAlpha "abc_$@" &isAlpha
println $isAlpha # false
call !string_IsAlpha "aAbBcCdDeEfF" &isAlpha
println $isAlpha # true
```

View File

@@ -0,0 +1,24 @@
# string_IsAlpha
Returns true if the given `string` is only numbers.
## Arguments
- str (string): the string to check.
## Returns
isAlpha (bool): returns `true` if `string` is only numbers, otherwise `false`.
## Example
### Ground
```python
call !string_IsAlpha "65535" &isAlpha
println $isAlpha # true
call !string_IsAlpha "$!@/_ffff" &isAlpha
println $isAlpha # false
call !string_IsAlpha "1234" &isAlpha
println $isAlpha # true
call !string_IsAlpha "abcd123" &isAlpha
println $isAlpha # false
```

View File

@@ -0,0 +1,24 @@
# string_IsSpace
Returns true if the given `string` is whitespace.
## Arguments
- str (string): the string to check.
## Returns
isAlpha (bool): returns `true` if `string` is whitespace, otherwise `false`.
## Example
### Ground
```python
call !string_IsAlpha " " &isAlpha
println $isAlpha # true
call !string_IsAlpha " " &isAlpha
println $isAlpha # true
call !string_IsAlpha "abc_def" &isAlpha
println $isAlpha # false
call !string_IsAlpha " a" &isAlpha
println $isAlpha # false
```

21
string/docs/find/count.md Normal file
View File

@@ -0,0 +1,21 @@
# string_Count
Count the number of times `needle` appears in `haystack`.
## Arguments
- haystack (string): the string to search through.
- needle (string): the substring to count.
## Returns
count (int): the number of times `needle` appears in `haystack`.
## Example
### Ground
```python
call !string_Count "Hello, World!" "l" &count
println $count # 3
```
### Solstice
```c
puts string_Count("Hello, World!", "l") // 3
```

21
string/docs/find/find.md Normal file
View File

@@ -0,0 +1,21 @@
# string_Find
Look for the first occurence of `needle` in `haystack` and return its index in the string. If it isn't found, return -1.
## Arguments
- haystack (string): the string to search.
- needle (string): the substring to look for.
## Returns
index (int): the index of the first occurence of `needle` in `haystack`. If it isn't found, return -1.
## Example
### Ground
```python
call !string_Find "abcdefg" "d" &index
println $index # 3
```
### Solstice
```c
puts string_Find("abcdefg", "d") // 3
```

View File

@@ -0,0 +1,21 @@
# string_FindLast
Look for the last occurence of `needle` in `haystack` and return its index in the string. If it isn't found, return -1.
## Arguments
- haystack (string): the string to search.
- needle (string): the substring to look for.
## Returns
index (int): the index of the last occurence of `needle` in `haystack`. If it isn't found, return -1.
## Example
### Ground
```python
call !string_FindLast "abcabcabc" "b" &index
println $index # 7
```
### Solstice
```c
puts string_FindLast("abcabcabc", "b") // 7
```

View File

@@ -0,0 +1,23 @@
# string_Lower
Make a string lowercase.
## Arguments
- str (string): the string to make lowercase.
## Returns
lowerString (string): the string in lowercase.
## Raises
- `AllocFail`: raised if Ground failed to allocate memory for the new string.
## Example
### Ground
```python
call !string_Lower "my STRING" &lowerString
println $lowerString # "my string"
```
### Solstice
```c
puts string_Lower("my STRING") // "my string"
```

View File

@@ -0,0 +1,24 @@
# string_Repeat
Repeat a string `times` times.
## Arguments
- str (string): the string to repeat.
- times (int): the number of times to repeat the string.
## Returns
result (string): the string repeated the given number of times.
## Raises
- `AllocFail`: raised if Ground fails to allocate memory for the new string.
## Example
### Ground
```python
call !string_Repeat "hi " 5 &repeated
println $repeated # "hi hi hi hi hi "
```
### Solstice
```c
puts string_Repeat("hi ", 5) // "hi hi hi hi hi "
```

View File

@@ -0,0 +1,22 @@
# string_Replace
Replace all occurences of `from` in `str` with `to`.
## Arguments
- str (string): the string to run a find and replace on.
- from (string): the substring you want to replace.
- to (string): the thing to replace all occurences of `from` with.
## Returns
result (string): the resulting string after the find and replace.
## Example
### Ground
```python
call !string_Replace "abacabacacacabacacacabac" "c" "b" &result
println $result # "abababababababababababab"
```
### Solstice
```c
puts string_Replace("abacabacacacabacacacabac", "c", "b") // "abababababababababababab"
```

View File

@@ -0,0 +1,23 @@
# string_Reverse
Get the reversed version of a string.
## Arguments
- str (string): the string to reverse.
## Returns
result (string): the reversed string.
## Raises
- `AllocFail`: raised if Ground failed to allocate memory for the new string.
## Example
### Ground
```python
call !string_Reverse "Hello, World!" &reverse
println $reverse # "!dlroW, olleH"
```
### Solstice
```c
puts string_Reverse("Hello, World!") // "!dlroW, olleH"
```

View File

@@ -0,0 +1,27 @@
# string_Substring
Get a substring of a string.
## Arguments
- str (string): the string to get a substring of.
- start (int): starting index of the substring.
- end (int): ending index of the substring.
## Returns
substring (string): the substring of the given string.
## Raises
- `AllocFail`: raised if Ground failed to allocate memory for the new string.
- `EndBeforeStart`: raised if the `end` index is less than the `start` index.
- `OutOfBounds`: raised if either the `end` or `start` index is outside the bounds of the string.
## Example
### Ground
```python
call !string_Substring "Hello, World!" 1 3 &sub
println $sub # "ell"
```
### Solstice
```c
puts string_Substring("Hello, World!", 1, 3) // "ell"
```

View File

@@ -0,0 +1,23 @@
# string_Trim
Trim whitespace from both sides of a string.
## Arguments
- str (string): the string to trim.
## Returns
trimmed (string): the string with its whitespace removed.
## Raises
- `AllocFail`: raised if Ground failed to allocate memory for the new string.
## Example
### Ground
```python
call !string_Trim " aaabbb " &trimmed
println $trimmed # "aaabbb"
```
### Solstice
```c
puts string_Trim(" aaabbb ") // "aaabbb"
```

View File

@@ -0,0 +1,23 @@
# string_TrimLeft
Trim whitespace from the left side of a string.
## Arguments
- str (string): the string to trim.
## Returns
trimmed (string): the string with the whitespace removed on the left side.
## Raises
- `AllocFail`: raised if Ground failed to allocate memory for the new string.
## Example
### Ground
```python
call !string_TrimLeft " aaabbb " &trimmed
println $trimmed # "aaabbb "
```
### Solstice
```c
puts string_TrimLeft(" aaabbb ") // "aaabbb "
```

View File

@@ -0,0 +1,23 @@
# string_TrimRight
Trim whitespace from the right side of a string.
## Arguments
- str (string): the string to trim.
## Returns
trimmed (string): the string with the whitespace removed on the right side.
## Raises
- `AllocFail`: raised if Ground failed to allocate memory for the new string.
## Example
### Ground
```python
call !string_TrimRight " aaabbb " &trimmed
println $trimmed # " aaabbb"
```
### Solstice
```c
puts string_TrimRight(" aaabbb ") // " aaabbb"
```

View File

@@ -0,0 +1,23 @@
# string_Upper
Make a string uppercase.
## Arguments
- str (string): the string to make uppercase.
## Returns
upperString (string): the string in uppercase.
## Raises
- `AllocFail`: raised if Ground failed to allocate memory for the new string.
## Example
### Ground
```python
call !string_Upper "MY string" &upperString
println $upperString # "MY STRING"
```
### Solstice
```c
puts string_Upper("MY string") // "MY STRING"
```

BIN
string/main.so Executable file

Binary file not shown.

7
string/mineral.ini Normal file
View File

@@ -0,0 +1,7 @@
[package]
description = String utility library.
version = 1.0.0
config_version = 1
[dependencies]