refactor: changes to keep up with compiler
This commit is contained in:
parent
3b5f26eab1
commit
c43361dbb7
@ -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 {
|
function translate_token_type(tt: TokenType, value: string): string {
|
||||||
const keywords = ["throws", "extends", "constructor", "case", "static", "const",
|
const keywords = ["throws", "extends", "constructor", "static", "const",
|
||||||
"enum", "union", "loop", "use", "break", "catch", "continue", "as", "do",
|
"enum", "union", "use", "break", "catch", "continue", "as", "do",
|
||||||
"finally", "for", "fun", "in", "fn", "nil", "return", "throw",
|
"finally", "fun", "fn", "nil", "return", "throw",
|
||||||
"try", "while", "type", "match", "with", "of", "abstract", "class", "interface",
|
"try", "type", "with", "of", "abstract", "class", "interface",
|
||||||
"private", "protected", "pub", "override", "open", "init", "val", "var", "mut", "clone"];
|
"private", "protected", "pub", "override", "open", "init", "val", "var", "mut", "clone"];
|
||||||
|
|
||||||
switch (tt) {
|
switch (tt) {
|
||||||
@ -222,6 +222,12 @@ function translate_token_type(tt: TokenType, value: string): string {
|
|||||||
case "FUN":
|
case "FUN":
|
||||||
case "IF":
|
case "IF":
|
||||||
case "ELSE":
|
case "ELSE":
|
||||||
|
case "FOR":
|
||||||
|
case "IN":
|
||||||
|
case "WHILE":
|
||||||
|
case "LOOP":
|
||||||
|
case "MATCH":
|
||||||
|
case "CASE":
|
||||||
return "keyword";
|
return "keyword";
|
||||||
default:
|
default:
|
||||||
return tt;
|
return tt;
|
||||||
|
@ -11,30 +11,37 @@ export interface Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type TokenType =
|
export type TokenType =
|
||||||
"Identifier" |
|
| "Identifier"
|
||||||
"Datatype" |
|
| "Datatype"
|
||||||
"Int" |
|
| "Int"
|
||||||
"Float" |
|
| "Float"
|
||||||
"String" |
|
| "String"
|
||||||
"Operator" |
|
| "Operator"
|
||||||
"LeftParen" |
|
| "LeftParen"
|
||||||
"RightParen" |
|
| "RightParen"
|
||||||
"LeftBracket" |
|
| "LeftBracket"
|
||||||
"RightBracket" |
|
| "RightBracket"
|
||||||
"LeftBrace" |
|
| "LeftBrace"
|
||||||
"RightBrace" |
|
| "RightBrace"
|
||||||
"NewLine" |
|
| "NewLine"
|
||||||
"Comment" |
|
| "Comment"
|
||||||
"MultilineComment" |
|
| "MultilineComment"
|
||||||
"Comma" |
|
| "Comma"
|
||||||
"INDENT" |
|
| "INDENT"
|
||||||
"DEDENT" |
|
| "DEDENT"
|
||||||
"VAL" |
|
| "VAL"
|
||||||
"VAR" |
|
| "VAR"
|
||||||
"EOF" |
|
| "EOF"
|
||||||
"FUN" |
|
| "FUN"
|
||||||
"IF" |
|
| "IF"
|
||||||
"ELSE";
|
| "ELSE"
|
||||||
|
| "ELSE"
|
||||||
|
| "FOR"
|
||||||
|
| "IN"
|
||||||
|
| "WHILE"
|
||||||
|
| "MATCH"
|
||||||
|
| "CASE"
|
||||||
|
;
|
||||||
|
|
||||||
export interface Err {
|
export interface Err {
|
||||||
Lex?: LexError
|
Lex?: LexError
|
||||||
|
@ -60,6 +60,16 @@ Strings have interpolation with `{}`.
|
|||||||
print("Hello, {name}") // Hello, Rose
|
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
|
## Bool
|
||||||
|
|
||||||
|
@ -71,18 +71,16 @@ number xand 1
|
|||||||
|
|
||||||
## Strings
|
## Strings
|
||||||
|
|
||||||
TBD.
|
Strings **do not use `.`** for concatenation. They use `++`.
|
||||||
|
|
||||||
Strings **do not use `.`** for concatenation. They use `+`.
|
|
||||||
|
|
||||||
<Code thpcode={`
|
<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={`
|
<Code thpcode={`
|
||||||
"Hello " + 322 // This is an error
|
"Hello " ++ 322 // 322 will be converted to "322"
|
||||||
`} />
|
`} />
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,14 +8,16 @@ import Code from "../../../components/Code.astro"
|
|||||||
|
|
||||||
## For loop
|
## 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.
|
Braces are required.
|
||||||
|
|
||||||
|
### Loop over values
|
||||||
|
|
||||||
<Code thpcode={`
|
<Code thpcode={`
|
||||||
val numbers = [0, 1, 2, 3]
|
val numbers = [0, 1, 2, 3]
|
||||||
|
|
||||||
for #(index, number) in numbers
|
for number in numbers
|
||||||
{
|
{
|
||||||
print(number)
|
print(number)
|
||||||
}
|
}
|
||||||
@ -28,14 +30,38 @@ val dict = .{
|
|||||||
cherries: 3,
|
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}")
|
print("{key} => {value}")
|
||||||
}
|
}
|
||||||
`} />
|
`} />
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## While loop
|
## While loop
|
||||||
|
|
||||||
<Code thpcode={`
|
<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
|
## Labelled loops
|
||||||
|
|
||||||
|
TBD
|
||||||
|
|
||||||
You can give labels to loops, allowing you to `break` and `continue` in nested loops.
|
You can give labels to loops, allowing you to `break` and `continue` in nested loops.
|
||||||
|
|
||||||
<Code thpcode={`
|
<Code thpcode={`
|
||||||
|
Loading…
Reference in New Issue
Block a user