Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions res/examples/const/collide_with_parameter.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn foo(a) {
const a = 123; // out: NameError: name "a" is already defined
}
4 changes: 4 additions & 0 deletions res/examples/const/duplicate_local.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
const a = "value";
const a = "other"; // out: NameError: name "a" is already defined
}
10 changes: 10 additions & 0 deletions res/examples/const/in_middle_of_block.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
const a = "a";
print a; // out: a
const b = a + " b";
print b; // out: a b
const c = a + " c";
print c; // out: a c
const d = b + " d";
print d; // out: a b d
}
6 changes: 6 additions & 0 deletions res/examples/const/in_nested_block.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
const a = "outer";
{
print a; // out: outer
}
}
9 changes: 9 additions & 0 deletions res/examples/const/local_from_method.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const foo = "variable";

class Foo {
fn method() {
print foo;
}
}

Foo().method(); // out: variable
2 changes: 2 additions & 0 deletions res/examples/const/redeclare_global.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a = "1";
const a; // out: NameError: name "a" is already defined
2 changes: 2 additions & 0 deletions res/examples/const/redefine_global.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a = "1";
const a = "2"; // out: NameError: name "a" is already defined
9 changes: 9 additions & 0 deletions res/examples/const/scope_reuse_in_different_blocks.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
const a = "first";
print a; // out: first
}

{
const a = "second";
print a; // out: second
}
7 changes: 7 additions & 0 deletions res/examples/const/shadow_and_local.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
const a = "outer";
{
print a; // out: outer
let a = "inner"; // out: NameError: name "a" is already defined
}
}
6 changes: 6 additions & 0 deletions res/examples/const/shadow_global.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const a = "global";
{
let a = "shadow"; // out: NameError: name "a" is already defined
print a;
}
print a;
8 changes: 8 additions & 0 deletions res/examples/const/shadow_local.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
const a = "local";
{
let a = "shadow"; // out: NameError: name "a" is already defined
print a;
}
print a;
}
2 changes: 2 additions & 0 deletions res/examples/const/uninitialized.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a; // out: Error: const must be initialized with a value
print a;
1 change: 1 addition & 0 deletions res/examples/const/use_clock_as_var.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const clock = "new clock"; // out: NameError: name "clock" is already defined
2 changes: 2 additions & 0 deletions res/examples/const/use_false_as_var.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// out: SyntaxError: unexpected "false"
const false = "value";
3 changes: 3 additions & 0 deletions res/examples/const/use_global_in_initializer.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = "value";
const a = a; // out: NameError: name "a" is already defined
print a;
4 changes: 4 additions & 0 deletions res/examples/const/use_local_in_initializer.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// out: NameError: cannot access variable "a" in its own initializer
const a = a;
}
2 changes: 2 additions & 0 deletions res/examples/const/use_nil_as_var.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// out: SyntaxError: unexpected "nil"
const nil = "value";
2 changes: 2 additions & 0 deletions res/examples/const/use_this_as_var.locks
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// out: SyntaxError: unexpected "this"
const this = "value";
25 changes: 19 additions & 6 deletions res/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ pub Program: ast::Program = <stmts:Spanned<Decl>*> => ast::Program { <> };
Decl = {
DeclClass,
DeclFn,
DeclLetVar,
DeclLet,
DeclConst,
Stmt,
}

DeclClass: ast::Stmt = <class:StmtClass> => ast::Stmt::Class(<>);

StmtClass: ast::StmtClass =
"class" <name:identifier> <super_:("extends" <Spanned<ExprIdentifier>>)?> "{"
<fields:(<Spanned<StmtAssign>>)*>
<fields:(<Spanned<StmtLet>>)*>
<methods:(<Spanned<StmtFn>>)*>
"}" =>
ast::StmtClass { <> };
Expand Down Expand Up @@ -67,10 +68,21 @@ StmtFn: ast::StmtFn = {
}
}

DeclLetVar: ast::Stmt = <assign:StmtAssign> =>
ast::Stmt::Assign(<>);
DeclConst: ast::Stmt = <assign:StmtConst> =>
ast::Stmt::Const(<>);

StmtAssign: ast::StmtAssign = "let" <name:identifier> <value:("=" <ExprS>)?> ";" => ast::StmtAssign {
StmtConst: ast::StmtConst = "const" <name:identifier> <value:("=" <ExprS>)?> ";" => ast::StmtConst {
identifier: ast::Identifier {
name,
depth: None
},
value,
};

DeclLet: ast::Stmt = <assign:StmtLet> =>
ast::Stmt::Let(<>);

StmtLet: ast::StmtLet = "let" <name:identifier> <value:("=" <ExprS>)?> ";" => ast::StmtLet {
identifier: ast::Identifier {
name,
depth: None
Expand Down Expand Up @@ -111,7 +123,7 @@ StmtClosed: ast::Stmt = {
}

ForInit: Option<ast::StmtS> = {
<Spanned<DeclLetVar>> => Some(<>),
<Spanned<DeclLet>> => Some(<>),
<Spanned<StmtExpr>> => Some(<>),
";" => None,
}
Expand Down Expand Up @@ -365,6 +377,7 @@ extern {
"this" => lexer::Token::This,
"true" => lexer::Token::True,
"let" => lexer::Token::Let,
"const" => lexer::Token::Const,
"while" => lexer::Token::While,
"extends" => lexer::Token::Extends,
}
Expand Down
17 changes: 12 additions & 5 deletions src/syntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub enum Stmt {
If(Box<StmtIf>),
Print(StmtPrint),
Return(StmtReturn),
Assign(StmtAssign),
Let(StmtLet),
Const(StmtConst),
While(Box<StmtWhile>),
Error,
}
Expand All @@ -36,7 +37,8 @@ impl Debug for Stmt {
Self::If(arg0) => f.write_fmt(format_args!("{:#?}", arg0)),
Self::Print(arg0) => f.write_fmt(format_args!("{:#?}", arg0)),
Self::Return(arg0) => f.write_fmt(format_args!("{:#?}", arg0)),
Self::Assign(arg0) => f.write_fmt(format_args!("{:#?}", arg0)),
Self::Let(arg0) => f.write_fmt(format_args!("{:#?}", arg0)),
Self::Const(arg0) => f.write_fmt(format_args!("{:#?}", arg0)),
Self::While(arg0) => f.write_fmt(format_args!("{:#?}", arg0)),
Self::Error => write!(f, "Error"),
}
Expand All @@ -53,7 +55,7 @@ pub struct StmtClass {
pub name: String,
pub super_: Option<ExprS>,
pub methods: Vec<Spanned<StmtFn>>,
pub fields: Vec<Spanned<StmtAssign>>,
pub fields: Vec<Spanned<StmtLet>>,
}

/// An expression statement evaluates an expression and discards the result.
Expand Down Expand Up @@ -94,9 +96,14 @@ pub struct StmtReturn {
pub value: Option<ExprS>,
}

/// Statement that sets `var.name` to `value`
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAssign {
pub struct StmtLet {
pub identifier: Identifier,
pub value: Option<ExprS>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct StmtConst {
pub identifier: Identifier,
pub value: Option<ExprS>,
}
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ pub enum Token {
This,
#[token("true")]
True,
#[token("let")]
Let,
#[token("const")]
Const,
#[token("while")]
While,
#[token("extends")]
Expand Down
8 changes: 6 additions & 2 deletions src/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ pub fn parse(source: &str, offset: usize) -> Result<Program, Vec<ErrorS>> {
}
ParseError::UnrecognizedToken { token: (start, _, end), expected } => (
Error::SyntaxError(SyntaxError::UnrecognizedToken {
token: source[start..end].to_string(),
token: source[start - offset..end - offset].to_string(),
expected,
}),
start..end,
),
ParseError::User { error } => error,
}));

if errors.is_empty() { Ok(program) } else { Err(errors) }
if errors.is_empty() {
Ok(program)
} else {
Err(errors)
}
}
21 changes: 20 additions & 1 deletion src/vm/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,26 @@ impl Compiler {
}
self.emit_u8(op::RETURN, span);
}
Stmt::Assign(assign) => {
Stmt::Let(assign) => {
let name = &assign.identifier.name;
if self.is_global() {
let name = gc.alloc(name);
match &assign.value {
Some(value) => self.compile_expr(value, gc)?,
None => self.emit_u8(op::NIL, span),
}
self.emit_u8(op::DEFINE_GLOBAL, span);
self.emit_constant(name.into(), span)?;
} else {
self.declare_local(name, span)?;
match &assign.value {
Some(value) => self.compile_expr(value, gc)?,
None => self.emit_u8(op::NIL, span),
}
self.define_local();
}
}
Stmt::Const(assign) => {
let name = &assign.identifier.name;
if self.is_global() {
let name = gc.alloc(name);
Expand Down