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>)* <file> -> (<typedef> | <function> | <method>)*
<access> -> "+" | "#" | "-" <access> -> "+" | "#" | "-"
<typedef> -> [<access>] <typeIdentifier> ":" <type> <typedef> -> [<access>] <typeIdentifier> ":" <type> <constantDeclaration>*
<function> -> [<access>] <signature> ["=" <expression>] <function> -> [<access>] <signature> ["=" <expression>]
<method> -> [<access>] <typeIdentifier> "." <function> <method> -> [<access>] <typeIdentifier> "." <function>
@ -330,7 +330,7 @@ this without hand-writing a parser.
| <arrayType> | <arrayType>
| <structType> | <structType>
| <interfaceType> | <interfaceType>
<namedType> -> <typeIdentifier> <namedType> -> [<identifier> "::"] <typeIdentifier>
<pointerType> -> "*" <type> <pointerType> -> "*" <type>
<sliceType> -> "*" ":" <type> <sliceType> -> "*" ":" <type>
<arrayType> -> <intLiteral> ":" <type> <arrayType> -> <intLiteral> ":" <type>
@ -362,9 +362,10 @@ this without hand-writing a parser.
| <break> | <break>
| <return> | <return>
| <assignment> | <assignment>
| <constant>
<variable> -> <identifier> <variable> -> <identifier>
<declaration> -> <identifier> ":" <type> <declaration> -> <identifier> ":" <type>
<call> -> "[" <expression>+ "]" <call> -> [<identifier> "::"] "[" <expression>+ "]"
<subscript> -> "[" "." <expression> <expression> "]" <subscript> -> "[" "." <expression> <expression> "]"
<slice> -> "[" "\" <expression> <expression>? "/" <expression>? "]" <slice> -> "[" "\" <expression> <expression>? "/" <expression>? "]"
<length> -> "[" "#" <expression> "]" <length> -> "[" "#" <expression> "]"
@ -386,6 +387,7 @@ this without hand-writing a parser.
<break> -> "[" "break" [<expression>] "]" <break> -> "[" "break" [<expression>] "]"
<return> -> "[" "return" [<expression>] "]" <return> -> "[" "return" [<expression>] "]"
<assignment> -> <expression> "=" <expression> <assignment> -> <expression> "=" <expression>
<constant> -> <namedType> "." <identifier>
<intLiteral> -> /-?[1-9][0-9]*/ <intLiteral> -> /-?[1-9][0-9]*/
| /-?0[0-7]*/ | /-?0[0-7]*/
@ -397,15 +399,16 @@ this without hand-writing a parser.
<structLiteral> -> "(." <member>* ")" <structLiteral> -> "(." <member>* ")"
<booleanLiteral> -> "true" | "false" <booleanLiteral> -> "true" | "false"
<member> -> <identifier> ":" <expression> <member> -> <identifier> ":" <expression>
<matchCase> -> "|" <declaration> <expression> <constantDeclaration> -> "|" <identifier> ["=" <expression>]
<switchCase> -> "|" <expression> <expression> <matchCase> -> "|" <declaration> <expression>
<defaultCase> -> "*" <expression> <switchCase> -> "|" <expression> <expression>
<signature> -> "[" <identifier> <declaration>* "]" [":" <type>] <defaultCase> -> "*" <expression>
<identifier> -> /[a-z][A-Za-z]*/ <signature> -> "[" <identifier> <declaration>* "]" [":" <type>]
<typeIdentifier> -> /[A-Z][A-Za-z]*/ <identifier> -> /[a-z][A-Za-z]*/
<operator> -> "+" | "++" | "-" | "--" | "*" | "/" | "%" <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) Position () errors.Position { return this.Pos }
func (this *ConstantDeclaration) Type () Type { return this.Ty } func (this *ConstantDeclaration) Type () Type { return this.Ty }
func (this *ConstantDeclaration) String () string { 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) - StructArray: 31:24:340920:(. x:Int y:Int)
- String: *:U8 - String: *:U8
+ FileDescriptor: I32 + FileDescriptor: I32
| stdin 0 | stdin = 0
| stdout 1 | stdout = 1
| stderr 2`, | stderr = 2`,
// input // input
` `
BasicInt: Int BasicInt: Int
@ -34,9 +34,9 @@ StructArray: 31:24:340920:(.
y:Int) y:Int)
String: *:U8 String: *:U8
+ FileDescriptor: I32 + FileDescriptor: I32
| stdin 0 | stdin = 0
| stdout 1 | stdout = 1
| stderr 2 | stderr = 2
`) `)
} }

View File

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