Changed constant declaration syntax to not be ambiguous

This commit is contained in:
Sasha Koshka 2024-04-12 00:07:22 -04:00
parent b822d57757
commit 3485fa1820
4 changed files with 26 additions and 22 deletions

View File

@ -320,7 +320,7 @@ this without hand-writing a parser.
```
<file> -> (<typedef> | <function> | <method>)*
<access> -> "+" | "#" | "-"
<typedef> -> [<access>] <typeIdentifier> ":" <type>
<typedef> -> [<access>] <typeIdentifier> ":" <type> <constantDeclaration>*
<function> -> [<access>] <signature> ["=" <expression>]
<method> -> [<access>] <typeIdentifier> "." <function>
@ -330,7 +330,7 @@ this without hand-writing a parser.
| <arrayType>
| <structType>
| <interfaceType>
<namedType> -> <typeIdentifier>
<namedType> -> [<identifier> "::"] <typeIdentifier>
<pointerType> -> "*" <type>
<sliceType> -> "*" ":" <type>
<arrayType> -> <intLiteral> ":" <type>
@ -362,9 +362,10 @@ this without hand-writing a parser.
| <break>
| <return>
| <assignment>
| <constant>
<variable> -> <identifier>
<declaration> -> <identifier> ":" <type>
<call> -> "[" <expression>+ "]"
<call> -> [<identifier> "::"] "[" <expression>+ "]"
<subscript> -> "[" "." <expression> <expression> "]"
<slice> -> "[" "\" <expression> <expression>? "/" <expression>? "]"
<length> -> "[" "#" <expression> "]"
@ -386,6 +387,7 @@ this without hand-writing a parser.
<break> -> "[" "break" [<expression>] "]"
<return> -> "[" "return" [<expression>] "]"
<assignment> -> <expression> "=" <expression>
<constant> -> <namedType> "." <identifier>
<intLiteral> -> /-?[1-9][0-9]*/
| /-?0[0-7]*/
@ -397,15 +399,16 @@ this without hand-writing a parser.
<structLiteral> -> "(." <member>* ")"
<booleanLiteral> -> "true" | "false"
<member> -> <identifier> ":" <expression>
<matchCase> -> "|" <declaration> <expression>
<switchCase> -> "|" <expression> <expression>
<defaultCase> -> "*" <expression>
<signature> -> "[" <identifier> <declaration>* "]" [":" <type>]
<identifier> -> /[a-z][A-Za-z]*/
<typeIdentifier> -> /[A-Z][A-Za-z]*/
<operator> -> "+" | "++" | "-" | "--" | "*" | "/" | "%"
| "!!" | "||" | "&&" | "^^"
| "!" | "|" | "&" | "^" | "<<" | ">>"
| "<" | ">" | "<=" | ">=" | "="
<member> -> <identifier> ":" <expression>
<constantDeclaration> -> "|" <identifier> ["=" <expression>]
<matchCase> -> "|" <declaration> <expression>
<switchCase> -> "|" <expression> <expression>
<defaultCase> -> "*" <expression>
<signature> -> "[" <identifier> <declaration>* "]" [":" <type>]
<identifier> -> /[a-z][A-Za-z]*/
<typeIdentifier> -> /[A-Z][A-Za-z]*/
<operator> -> "+" | "++" | "-" | "--" | "*" | "/" | "%"
| "!!" | "||" | "&&" | "^^"
| "!" | "|" | "&" | "^" | "<<" | ">>"
| "<" | ">" | "<=" | ">=" | "="
```

View File

@ -267,5 +267,5 @@ type ConstantDeclaration struct {
func (this *ConstantDeclaration) Position () errors.Position { return this.Pos }
func (this *ConstantDeclaration) Type () Type { return this.Ty }
func (this *ConstantDeclaration) String () string {
return fmt.Sprint("| ", this.Name, " ", this.Value)
return fmt.Sprint("| ", this.Name, " = ", this.Value)
}

View File

@ -13,9 +13,9 @@ testString (test,
- StructArray: 31:24:340920:(. x:Int y:Int)
- String: *:U8
+ FileDescriptor: I32
| stdin 0
| stdout 1
| stderr 2`,
| stdin = 0
| stdout = 1
| stderr = 2`,
// input
`
BasicInt: Int
@ -34,9 +34,9 @@ StructArray: 31:24:340920:(.
y:Int)
String: *:U8
+ FileDescriptor: I32
| stdin 0
| stdout 1
| stderr 2
| stdin = 0
| stdout = 1
| stderr = 2
`)
}

View File

@ -186,7 +186,8 @@ func (this *treeParser) parseConstantDeclaration () (*entity.ConstantDeclaration
declaration.Name = this.Value()
this.Next()
if this.Is(startTokensExpression...) {
if this.Is(lexer.Symbol) && this.ValueIs("=") {
this.Next()
value, err := this.parseExpression()
if err != nil { return nil, err }
declaration.Value = value