refactor: changes to keep up with compiler

This commit is contained in:
Araozu 2024-08-29 11:01:23 -05:00
parent 3b5f26eab1
commit c43361dbb7
5 changed files with 87 additions and 55 deletions

View File

@ -191,10 +191,10 @@ function process_token_value_and_end(value: string, token_type: TokenType, first
}
function translate_token_type(tt: TokenType, value: string): string {
const keywords = ["throws", "extends", "constructor", "case", "static", "const",
"enum", "union", "loop", "use", "break", "catch", "continue", "as", "do",
"finally", "for", "fun", "in", "fn", "nil", "return", "throw",
"try", "while", "type", "match", "with", "of", "abstract", "class", "interface",
const keywords = ["throws", "extends", "constructor", "static", "const",
"enum", "union", "use", "break", "catch", "continue", "as", "do",
"finally", "fun", "fn", "nil", "return", "throw",
"try", "type", "with", "of", "abstract", "class", "interface",
"private", "protected", "pub", "override", "open", "init", "val", "var", "mut", "clone"];
switch (tt) {
@ -222,6 +222,12 @@ function translate_token_type(tt: TokenType, value: string): string {
case "FUN":
case "IF":
case "ELSE":
case "FOR":
case "IN":
case "WHILE":
case "LOOP":
case "MATCH":
case "CASE":
return "keyword";
default:
return tt;

View File

@ -11,30 +11,37 @@ export interface Token {
}
export type TokenType =
"Identifier" |
"Datatype" |
"Int" |
"Float" |
"String" |
"Operator" |
"LeftParen" |
"RightParen" |
"LeftBracket" |
"RightBracket" |
"LeftBrace" |
"RightBrace" |
"NewLine" |
"Comment" |
"MultilineComment" |
"Comma" |
"INDENT" |
"DEDENT" |
"VAL" |
"VAR" |
"EOF" |
"FUN" |
"IF" |
"ELSE";
| "Identifier"
| "Datatype"
| "Int"
| "Float"
| "String"
| "Operator"
| "LeftParen"
| "RightParen"
| "LeftBracket"
| "RightBracket"
| "LeftBrace"
| "RightBrace"
| "NewLine"
| "Comment"
| "MultilineComment"
| "Comma"
| "INDENT"
| "DEDENT"
| "VAL"
| "VAR"
| "EOF"
| "FUN"
| "IF"
| "ELSE"
| "ELSE"
| "FOR"
| "IN"
| "WHILE"
| "MATCH"
| "CASE"
;
export interface Err {
Lex?: LexError

View File

@ -60,6 +60,16 @@ Strings have interpolation with `{}`.
print("Hello, {name}") // Hello, Rose
`} />
Unlike PHP, THP strings are concatenated with `++`, not with `.`.
This new operator implicitly converts any operator into a string.
<Code thpcode={`
val name = "John" ++ " " ++ "Doe"
val greeting = "My name is " ++ name ++ " and I'm " ++ 32 ++ " years old"
`} />
The plus operator `+` is reserved for numbers.
## Bool

View File

@ -71,18 +71,16 @@ number xand 1
## Strings
TBD.
Strings **do not use `.`** for concatenation. They use `+`.
Strings **do not use `.`** for concatenation. They use `++`.
<Code thpcode={`
"Hello " + "world."
"Hello " ++ "world."
`} />
However, the plus operator `+` does not implicitly convert types.
This new operator **implicitly converts** types to string
<Code thpcode={`
"Hello " + 322 // This is an error
"Hello " ++ 322 // 322 will be converted to "322"
`} />

View File

@ -8,14 +8,16 @@ import Code from "../../../components/Code.astro"
## For loop
This is simmilar to PHP's `foreach`. There is no equivalent to PHP's `for`.
THP loops are similar to PHP's `foreach`. There is no equivalent to PHP's `for`.
Braces are required.
### Loop over values
<Code thpcode={`
val numbers = [0, 1, 2, 3]
for #(index, number) in numbers
for number in numbers
{
print(number)
}
@ -28,14 +30,38 @@ val dict = .{
cherries: 3,
}
for #(key, value) in dict
for value in dict
{
print("{value}")
}
`} />
### Loop over keys and values
<Code thpcode={`
val numbers = [0, 1, 2, 3]
for index, number in numbers
{
print("{index} : {number}")
}
`} />
<Code thpcode={`
val dict = .{
apple: 10,
banana: 7,
cherries: 3,
}
for key, value in dict
{
print("{key} => {value}")
}
`} />
## While loop
<Code thpcode={`
@ -50,25 +76,10 @@ while index < colors.size()
`} />
## Infinite loop
Instead of doing `while true {}` use `loop`.
<Code thpcode={`
loop
{
print("looping")
if condition
{
break
}
}
`} />
## Labelled loops
TBD
You can give labels to loops, allowing you to `break` and `continue` in nested loops.
<Code thpcode={`