Merge pull request 'implement-reserved-identifiers' (#76) from implement-reserved-identifiers into main

Reviewed-on: #76
This commit is contained in:
Sasha Koshka 2024-04-02 07:00:59 +00:00
commit 2a6f086350
4 changed files with 30 additions and 4 deletions

View File

@ -103,7 +103,7 @@ U: (| Int F64 UInt)
func TestSwitch (test *testing.T) {
testString (test,
`
[switch x:Int]:Int = switch x
[f x:Int]:Int = switch x
| 0 5
| 1 4
| 2 3
@ -134,7 +134,7 @@ func TestSwitchErrBadLiteral (test *testing.T) {
testStringErr (test,
"cannot use array literal as Int", 4, 4,
`
[switch x:Int]:Int = switch x
[f x:Int]:Int = switch x
| 0 5
| (1) 4
| 2 3
@ -146,7 +146,7 @@ func TestSwitchErrNotConstant (test *testing.T) {
testStringErr (test,
"y cannot represent a constant integer", 7, 4,
`
[switch x:Int]:Int = {
[f x:Int]:Int = {
y:Int = 1
switch x
@ -162,7 +162,7 @@ func TestSwitchErrDuplicate (test *testing.T) {
testStringErr (test,
"65 already listed in match at stream0.fspl:6:2", 7, 4,
`
[switch x:I8]:Int = switch x
[f x:I8]:Int = switch x
| 0 5
| 1 4
| 2 3

View File

@ -3,6 +3,7 @@ package analyzer
import "fmt"
import "git.tebibyte.media/fspl/fspl/errors"
import "git.tebibyte.media/fspl/fspl/entity"
import "git.tebibyte.media/fspl/fspl/parser/fspl"
// All expression analysis routines must take in the type they are being
// assigned to and return an error if they can't be assigned to it. if the type
@ -62,6 +63,13 @@ func (this *Tree) analyzeDeclaration (
entity.Expression,
error,
) {
if fsplParser.IsReserved(declaration.Name) {
return nil, errors.Errorf (
declaration.Position(),
"cannot shadow reserved identifier %s",
declaration.Name)
}
scope, _ := this.topScope()
existing := scope.Variable(declaration.Name)
if existing != nil {

View File

@ -76,6 +76,11 @@ func (this *Tree) assembleRawMaps () error {
}
func (this *Tree) topLevelNameAvailable (currentPos errors.Position, key entity.Key) error {
if fsplParser.IsReserved(key.Name) {
return errors.Errorf (
currentPos, "cannot shadow reserved identifier %s",
key.Name)
}
if _, isPrimitive := primitiveTypes[key.Name]; isPrimitive {
return errors.Errorf (
currentPos, "cannot shadow primitive %s",

13
parser/fspl/reserved.go Normal file
View File

@ -0,0 +1,13 @@
package fsplParser
func IsReserved (identifier string) bool {
switch identifier {
case "true", "false", "nil",
"if", "match", "switch",
"loop", "for",
"then", "in",
"break", "return":
return true
default: return false
}
}