diff --git a/src/lexer/lexer.sols b/src/lexer/lexer.sols index ce077ba..d6f64b0 100644 --- a/src/lexer/lexer.sols +++ b/src/lexer/lexer.sols @@ -1,16 +1,97 @@ 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 { - input = "" + sourceCode = "x = 123" current = 0 - constructor(string input) { - self.input = input + private def getTokenType(string input) SolsToken { + return KEYWORDS.get(input) } - def lex() List { - output = List(1) + def lex() { + inString = false - return output + lineNum = 1 + lineStart = 0 + currentLine = "" + + while lineStart < string_Length() { + + } } -} \ No newline at end of file +} + + + +lexer = new SolsLexer +puts lexer.getTokenType("if") \ No newline at end of file diff --git a/string/SUMMARY.md b/string/SUMMARY.md new file mode 100644 index 0000000..5b29ed4 --- /dev/null +++ b/string/SUMMARY.md @@ -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. diff --git a/string/docs/char_at.md b/string/docs/char_at.md new file mode 100644 index 0000000..832bf11 --- /dev/null +++ b/string/docs/char_at.md @@ -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" +``` \ No newline at end of file diff --git a/string/docs/check/contains.md b/string/docs/check/contains.md new file mode 100644 index 0000000..6ac3178 --- /dev/null +++ b/string/docs/check/contains.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/check/ends_with.md b/string/docs/check/ends_with.md new file mode 100644 index 0000000..3c9fe2a --- /dev/null +++ b/string/docs/check/ends_with.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/check/starts_with.md b/string/docs/check/starts_with.md new file mode 100644 index 0000000..acd8095 --- /dev/null +++ b/string/docs/check/starts_with.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/classify/is_alnum.md b/string/docs/classify/is_alnum.md new file mode 100644 index 0000000..5e2fab4 --- /dev/null +++ b/string/docs/classify/is_alnum.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/classify/is_alpha.md b/string/docs/classify/is_alpha.md new file mode 100644 index 0000000..02577e2 --- /dev/null +++ b/string/docs/classify/is_alpha.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/classify/is_digit copy.md b/string/docs/classify/is_digit copy.md new file mode 100644 index 0000000..e6b8ccf --- /dev/null +++ b/string/docs/classify/is_digit copy.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/classify/is_space.md b/string/docs/classify/is_space.md new file mode 100644 index 0000000..6ee730f --- /dev/null +++ b/string/docs/classify/is_space.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/find/count.md b/string/docs/find/count.md new file mode 100644 index 0000000..02ba699 --- /dev/null +++ b/string/docs/find/count.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/find/find.md b/string/docs/find/find.md new file mode 100644 index 0000000..99ed30b --- /dev/null +++ b/string/docs/find/find.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/find/find_last.md b/string/docs/find/find_last.md new file mode 100644 index 0000000..35f772d --- /dev/null +++ b/string/docs/find/find_last.md @@ -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 +``` \ No newline at end of file diff --git a/string/docs/modify/lower.md b/string/docs/modify/lower.md new file mode 100644 index 0000000..c501c82 --- /dev/null +++ b/string/docs/modify/lower.md @@ -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" +``` \ No newline at end of file diff --git a/string/docs/modify/repeat.md b/string/docs/modify/repeat.md new file mode 100644 index 0000000..091d351 --- /dev/null +++ b/string/docs/modify/repeat.md @@ -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 " +``` \ No newline at end of file diff --git a/string/docs/modify/replace.md b/string/docs/modify/replace.md new file mode 100644 index 0000000..c44b73a --- /dev/null +++ b/string/docs/modify/replace.md @@ -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" +``` \ No newline at end of file diff --git a/string/docs/modify/reverse.md b/string/docs/modify/reverse.md new file mode 100644 index 0000000..2ac921c --- /dev/null +++ b/string/docs/modify/reverse.md @@ -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" +``` \ No newline at end of file diff --git a/string/docs/modify/substring.md b/string/docs/modify/substring.md new file mode 100644 index 0000000..e46940d --- /dev/null +++ b/string/docs/modify/substring.md @@ -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" +``` \ No newline at end of file diff --git a/string/docs/modify/trim.md b/string/docs/modify/trim.md new file mode 100644 index 0000000..88e822b --- /dev/null +++ b/string/docs/modify/trim.md @@ -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" +``` \ No newline at end of file diff --git a/string/docs/modify/trim_left.md b/string/docs/modify/trim_left.md new file mode 100644 index 0000000..add1055 --- /dev/null +++ b/string/docs/modify/trim_left.md @@ -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 " +``` \ No newline at end of file diff --git a/string/docs/modify/trim_right.md b/string/docs/modify/trim_right.md new file mode 100644 index 0000000..f884b03 --- /dev/null +++ b/string/docs/modify/trim_right.md @@ -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" +``` \ No newline at end of file diff --git a/string/docs/modify/upper.md b/string/docs/modify/upper.md new file mode 100644 index 0000000..5e7a5d1 --- /dev/null +++ b/string/docs/modify/upper.md @@ -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" +``` \ No newline at end of file diff --git a/string/main.so b/string/main.so new file mode 100755 index 0000000..91dcf73 Binary files /dev/null and b/string/main.so differ diff --git a/string/mineral.ini b/string/mineral.ini new file mode 100644 index 0000000..e2365e0 --- /dev/null +++ b/string/mineral.ini @@ -0,0 +1,7 @@ +[package] +description = String utility library. +version = 1.0.0 +config_version = 1 + +[dependencies] +