Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constant variables to E2 #2769

Merged
merged 1 commit into from
Oct 4, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## SHOULD_FAIL:COMPILE

const X = 5

X = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## SHOULD_FAIL:COMPILE

const Y = 2
const Y = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## SHOULD_FAIL:COMPILE

const X = 5
local X = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## SHOULD_FAIL:COMPILE

const Z = "Foo"
A=Z="Y"
4 changes: 4 additions & 0 deletions data/expression2/tests/compiler/parser/const.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## SHOULD_PASS:COMPILE

const Var = 5
const Xyz = "test"
16 changes: 15 additions & 1 deletion lua/entities/gmod_wire_expression2/base/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
---@field function { [1]: string, [2]: EnvFunction}?
---@field ops integer

---@alias VarData { type: string, trace_if_unused: Trace?, initialized: boolean, depth: integer }
---@alias VarData { type: string, trace_if_unused: Trace?, const: boolean, initialized: boolean, depth: integer }

---@class Scope
---@field parent Scope?
Expand Down Expand Up @@ -378,7 +378,7 @@

---@param data { [1]: Token<string>, [2]: Node, [3]: Node, [4]: Node?, [5]: Node } var start stop step block
[NodeVariant.For] = function (self, trace, data)
local var, start, stop, step = data[1], self:CompileExpr(data[2]), self:CompileExpr(data[3]), data[4] and self:CompileExpr(data[4]) or data[4]

Check warning on line 381 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: step

local block = self:Scope(function(scope)
scope.data.loop = true
Expand Down Expand Up @@ -917,6 +917,8 @@
-- Local declaration. Fastest case.
local var_name = data[2][1][1].value
self:AssertW(not self.scope.vars[var_name], "Do not redeclare existing variable " .. var_name, trace)
self:Assert(not self.scope.vars[var_name] or not self.scope.vars[var_name].const, "Cannot redeclare constant variable", trace)

self.scope:DeclVar(var_name, { initialized = true, trace_if_unused = data[2][1][1].trace, type = value_ty })
return function(state) ---@param state RuntimeContext
state.Scope[var_name] = value(state)
Expand Down Expand Up @@ -973,6 +975,7 @@
end
else
self:Assert(existing.type == value_ty, "Cannot assign type (" .. value_ty .. ") to variable of type (" .. existing.type .. ")", trace)
self:Assert(not existing.const, "Cannot assign to constant variable " .. var, trace)
existing.initialized = true

local id = existing.depth
Expand Down Expand Up @@ -1029,6 +1032,17 @@
end
end,

---@param data { [1]: Token<string>, [2]: Node }
[NodeVariant.Const] = function (self, trace, data)
local name, expr, expr_ty = data[1].value, self:CompileExpr(data[2])
self:Assert(not self.scope.vars[name], "Cannot redeclare existing variable " .. name, trace)
self.scope:DeclVar(name, { type = expr_ty, initialized = true, const = true, trace_if_unused = data[1].trace })

return function(state)
state.Scope[name] = expr(state)
end
end,

---@param data Token<string>
[NodeVariant.Increment] = function (self, trace, data)
-- Transform V-- to V = V + 1
Expand Down
59 changes: 35 additions & 24 deletions lua/entities/gmod_wire_expression2/base/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

local Trace, Warning, Error = E2Lib.Debug.Trace, E2Lib.Debug.Warning, E2Lib.Debug.Error
local Tokenizer = E2Lib.Tokenizer
local Token, TokenVariant = Tokenizer.Token, Tokenizer.Variant

Check warning on line 17 in lua/entities/gmod_wire_expression2/base/parser.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: Token
local Keyword, Grammar, Operator = E2Lib.Keyword, E2Lib.Grammar, E2Lib.Operator

---@class Parser
Expand Down Expand Up @@ -70,35 +70,36 @@
Decrement = 10, -- `--`
CompoundArithmetic = 11, -- `+=`, `-=`, `*=`, `/=`
Assignment = 12, -- `X = Y[2, number] = Z[2] = 5` or `local X = 5`
Const = 13, -- const X = 5

Switch = 13, -- `switch (<EXPR>) { case <EXPR>, <STMT>* default, <STMT*> }
Function = 14, -- `function test() {}`
Include = 15, -- #include "file"
Try = 16, -- try {} catch (Err) {}
Switch = 14, -- `switch (<EXPR>) { case <EXPR>, <STMT>* default, <STMT*> }
Function = 15, -- `function test() {}`
Include = 16, -- #include "file"
Try = 17, -- try {} catch (Err) {}

--- Compile time constructs
Event = 17, -- event tick() {}
Event = 18, -- event tick() {}

--- Expressions
ExprTernary = 18, -- `X ? Y : Z`
ExprDefault = 19, -- `X ?: Y`
ExprLogicalOp = 20, -- `|` `&` (Yes they are flipped.)
ExprBinaryOp = 21, -- `||` `&&` `^^`
ExprComparison = 22, -- `>` `<` `>=` `<=`
ExprEquals = 23, -- `==` `!=`
ExprBitShift = 24, -- `>>` `<<`
ExprArithmetic = 25, -- `+` `-` `*` `/` `^` `%`
ExprUnaryOp = 26, -- `-` `+` `!`
ExprMethodCall = 27, -- `<EXPR>:call()`
ExprIndex = 28, -- `<EXPR>[<EXPR>, <type>?]`
ExprGrouped = 29, -- (<EXPR>)
ExprCall = 30, -- `call()`
ExprStringCall = 31, -- `""()` (Temporary until lambdas are made)
ExprUnaryWire = 32, -- `~Var` `$Var` `->Var`
ExprArray = 33, -- `array(1, 2, 3)` or `array(1 = 2, 2 = 3)`
ExprTable = 34, -- `table(1, 2, 3)` or `table(1 = 2, "test" = 3)`
ExprLiteral = 35, -- `"test"` `5e2` `4.023` `4j`
ExprIdent = 36 -- `Variable`
ExprTernary = 19, -- `X ? Y : Z`
ExprDefault = 20, -- `X ?: Y`
ExprLogicalOp = 21, -- `|` `&` (Yes they are flipped.)
ExprBinaryOp = 22, -- `||` `&&` `^^`
ExprComparison = 23, -- `>` `<` `>=` `<=`
ExprEquals = 24, -- `==` `!=`
ExprBitShift = 25, -- `>>` `<<`
ExprArithmetic = 26, -- `+` `-` `*` `/` `^` `%`
ExprUnaryOp = 27, -- `-` `+` `!`
ExprMethodCall = 28, -- `<EXPR>:call()`
ExprIndex = 29, -- `<EXPR>[<EXPR>, <type>?]`
ExprGrouped = 30, -- (<EXPR>)
ExprCall = 31, -- `call()`
ExprStringCall = 32, -- `""()` (Temporary until lambdas are made)
ExprUnaryWire = 33, -- `~Var` `$Var` `->Var`
ExprArray = 34, -- `array(1, 2, 3)` or `array(1 = 2, 2 = 3)`
ExprTable = 35, -- `table(1, 2, 3)` or `table(1 = 2, "test" = 3)`
ExprLiteral = 36, -- `"test"` `5e2` `4.023` `4j`
ExprIdent = 37 -- `Variable`
}

Parser.Variant = NodeVariant
Expand Down Expand Up @@ -348,6 +349,16 @@
self.index = self.index - 1
end

if self:Consume(TokenVariant.Keyword, Keyword.Const) then
local trace = self:Prev().trace

local name = self:Assert(self:Consume(TokenVariant.Ident), "Expected variable name after const")
self:Assert( self:Consume(TokenVariant.Operator, Operator.Ass), "Expected = for constant declaration" )
local value = self:Assert(self:Expr(), "Expected expression for constant declaration")

return Node.new(NodeVariant.Const, { name, value }, trace:stitch(self:Prev().trace))
end

local is_local, var = self:Consume(TokenVariant.Keyword, Keyword.Local), self:Consume(TokenVariant.Ident)
if not var then
self:Assert(not is_local, "Invalid operator (local) must be used for variable declaration.")
Expand Down Expand Up @@ -629,7 +640,7 @@
if variadic then
self:Assert( self:Consume(TokenVariant.Grammar, Grammar.RParen), "Variadic parameter must be final in list" )
return params
elseif self:Consume(TokenVariant.Grammar, Grammar.Comma) then

Check warning on line 643 in lua/entities/gmod_wire_expression2/base/parser.lua

View workflow job for this annotation

GitHub Actions / lint

"Empty block"

Empty elseif statement
elseif self:Consume(TokenVariant.Grammar, Grammar.RParen) then
return params
else
Expand Down
32 changes: 17 additions & 15 deletions lua/entities/gmod_wire_expression2/core/e2lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
local new_signature = string.format("%s(%s)", funcname, table.concat(args, ","))
if thistype then new_signature = thistype .. ":" .. new_signature end

return (not rets or rets == "") and (new_signature) or (E2Lib.typeName(rets) .. "=" .. new_signature)

Check warning on line 172 in lua/entities/gmod_wire_expression2/core/e2lib.lua

View workflow job for this annotation

GitHub Actions / lint

"Unnecessary parentheses"

Unnecessary parentheses
end

-- ------------------------ various entity checkers ----------------------------
Expand Down Expand Up @@ -420,36 +420,38 @@
Else = 3,
-- ``local``
Local = 4,
-- ``const``
Const = 5,
-- ``while``
While = 5,
While = 6,
-- ``for``
For = 6,
For = 7,
-- ``break``
Break = 7,
Break = 8,
-- ``continue``
Continue = 8,
Continue = 9,
-- ``switch``
Switch = 9,
Switch = 10,
-- ``case``
Case = 10,
Case = 11,
-- ``default``
Default = 11,
Default = 12,
-- ``foreach``
Foreach = 12,
Foreach = 13,
-- ``function``
Function = 13,
Function = 14,
-- ``return``
Return = 14,
Return = 15,
-- ``#include``
["#Include"] = 15,
["#Include"] = 16,
-- ``try``
Try = 16,
Try = 17,
-- ``catch``
Catch = 17,
Catch = 18,
-- ``do``
Do = 18,
Do = 19,
-- ``event``
Event = 19
Event = 20
}

E2Lib.Keyword = Keyword
Expand Down Expand Up @@ -1198,7 +1200,7 @@
local status, tree, dvars = E2Lib.Parser.Execute(tokens)
if not status then return false, tree end

local status, script, inst = E2Lib.Compiler.Execute(tree, directives, dvars, {})

Check warning on line 1203 in lua/entities/gmod_wire_expression2/core/e2lib.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: inst
if not status then return false, script end

local ctx = RuntimeContext.builder()
Expand Down
1 change: 1 addition & 0 deletions lua/wire/client/text_editor/modes/e2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
["function"] = { [true] = true },
["return"] = { [true] = true },
["local"] = { [true] = true },
["const"] = { [true] = true },
["try"] = { [true] = true },
["do"] = { [true] = true },
["event"] = { [true] = true },
Expand Down Expand Up @@ -196,7 +197,7 @@

local str = string_gsub( table_concat( self.Rows, "\n", 1, self.Scroll[1]-1 ), "\r", "" )

for pos, char in string_gmatch( str, '()([#"\n])' ) do

Check warning on line 200 in lua/wire/client/text_editor/modes/e2.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'single quoted strings' and 'double quoted strings'
if not self.blockcomment and not self.multilinestring and not singlelinecomment then
if char == '"' then
self.multilinestring = true
Expand Down
Loading