chharvey / counterpoint

A robust programming language.
GNU Affero General Public License v3.0
2 stars 0 forks source link

Semantic Errors: Invalid References or Assignments #14

Closed chharvey closed 3 years ago

chharvey commented 4 years ago

If references or (re)assignments are invalid, the compiler should throw a static semantic error.

Errors should be thrown in the following situations:

These are semantic errors, as they are not enforceable via syntax/parsing.

Undeclared Variable Reference

If a variable is referenced but has not been declared, a ReferenceError will be thrown.

let x = y; \ ReferenceError (y is not declared)

Temporal Dead Zone

Similarly, if a variable is referenced lexically before it is declared (above, in source order), a ReferenceError will be thrown. The zone between where a variable is referenced and where it is declared below is called the “temporal dead zone”.

let x = y; \ ReferenceError (y is used before it is declared)
"
a temporal dead zone!
"
let y = 2;

Duplicate Declaration

If a variable is declared more than once, an AssignmentError will be thrown.

let x = 2;
let x = 4; \ AssignmentError (duplicate variable declaration)

Fixed Variable Reassignment

If a variable is not declared with unfixed, it is a fixed variable. If a fixed variable is reassigned, an AssignmentError will be thrown.

let x = 2;
x = 4; \ AssignmentError (reassignment of a fixed variable)

Aside: Reassigning Unfixed Variables

Variables declared with unfixed may be reassigned.

let unfixed x = 2;
x = 4; \ no compile-time error