Skip to content
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
2 changes: 1 addition & 1 deletion lib/lua/ast/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Lua.AST.Block do
%Lua.AST.Block{stmts: [], meta: nil}

iex> Lua.AST.Block.new([], %Lua.AST.Meta{})
%Lua.AST.Block{stmts: [], meta: %Lua.AST.Meta{start: nil, end: nil, metadata: %{}}}
%Lua.AST.Block{stmts: [], meta: %Lua.AST.Meta{start: nil, end: nil, metadata: %{}, id: nil}}
"""
@spec new([Statement.t()], Meta.t() | nil) :: t()
def new(stmts \\ [], meta \\ nil) do
Expand Down
238 changes: 238 additions & 0 deletions lib/lua/ast/ids.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
defmodule Lua.AST.Ids do
@moduledoc """
Stamps AST nodes with chunk-unique identifiers.

Compiler passes keep per-node tables — which registers a `local` claimed,
which upvalue a `Var` resolved to, the close-upvalue watermark of a block.
Keying those tables by the node term makes every read and write hash the
node's whole subtree, so the deepest nodes (function bodies, blocks) — the
ones that make the most useful keys — are also the most expensive ones.

`assign/1` walks a parsed chunk once and writes a distinct integer into
each node's `meta.id`, letting those tables key on a single word instead.
Ids are unique across the whole chunk, including the bodies of nested
functions.
"""
Comment on lines +2 to +15

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe we want this as a public-facing module. We should follow the pattern for other internal modules

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this might be handled by the config in mix.exs


alias Lua.AST.Block
alias Lua.AST.Chunk
alias Lua.AST.Expr
alias Lua.AST.Meta
alias Lua.AST.Statement

@doc """
Returns `chunk` with every reachable node stamped with a unique `meta.id`.

Nodes the parser built without metadata gain a `Lua.AST.Meta` carrying only
the id; nodes that already have one keep their positions and comments.
"""
@spec assign(Chunk.t()) :: Chunk.t()
def assign(%Chunk{} = chunk) do
{chunk, _next_id} = number(chunk, 0)
chunk
end

# Children are numbered before their parent, so a node's id is always
# greater than every id inside it. Nothing relies on that ordering; it just
# falls out of numbering on the way back up.

defp number(%Chunk{block: block} = node, next) do
{block, next} = number(block, next)
{%{node | block: block, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Block{stmts: stmts} = node, next) do
{stmts, next} = number_list(stmts, next, [])
{%{node | stmts: stmts, meta: with_id(node.meta, next)}, next + 1}
end

# Expressions

defp number(%Expr.Nil{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}
defp number(%Expr.Bool{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}
defp number(%Expr.Number{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}
defp number(%Expr.String{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}
defp number(%Expr.Var{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}
defp number(%Expr.Vararg{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}

defp number(%Expr.BinOp{left: left, right: right} = node, next) do
{left, next} = number(left, next)
{right, next} = number(right, next)
{%{node | left: left, right: right, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Expr.UnOp{operand: operand} = node, next) do
{operand, next} = number(operand, next)
{%{node | operand: operand, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Expr.Table{fields: fields} = node, next) do
{fields, next} = number_table_fields(fields, next, [])
{%{node | fields: fields, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Expr.Call{func: func, args: args} = node, next) do
{func, next} = number(func, next)
{args, next} = number_list(args, next, [])
{%{node | func: func, args: args, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Expr.MethodCall{object: object, args: args} = node, next) do
{object, next} = number(object, next)
{args, next} = number_list(args, next, [])
{%{node | object: object, args: args, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Expr.Index{table: table, key: key} = node, next) do
{table, next} = number(table, next)
{key, next} = number(key, next)
{%{node | table: table, key: key, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Expr.Property{table: table} = node, next) do
{table, next} = number(table, next)
{%{node | table: table, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Expr.Function{body: body} = node, next) do
{body, next} = number(body, next)
{%{node | body: body, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Expr.Paren{inner: inner} = node, next) do
{inner, next} = number(inner, next)
{%{node | inner: inner, meta: with_id(node.meta, next)}, next + 1}
end

# Statements

defp number(%Statement.Break{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}
defp number(%Statement.Goto{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}
defp number(%Statement.Label{} = node, next), do: {%{node | meta: with_id(node.meta, next)}, next + 1}

defp number(%Statement.Assign{targets: targets, values: values} = node, next) do
{targets, next} = number_list(targets, next, [])
{values, next} = number_list(values, next, [])
{%{node | targets: targets, values: values, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.Local{values: values} = node, next) do
{values, next} = number_list(values, next, [])
{%{node | values: values, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.LocalFunc{body: body} = node, next) do
{body, next} = number(body, next)
{%{node | body: body, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.FuncDecl{body: body} = node, next) do
{body, next} = number(body, next)
{%{node | body: body, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.CallStmt{call: call} = node, next) do
{call, next} = number(call, next)
{%{node | call: call, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.If{} = node, next) do
%Statement.If{condition: condition, then_block: then_block, elseifs: elseifs, else_block: else_block} = node

{condition, next} = number(condition, next)
{then_block, next} = number(then_block, next)
{elseifs, next} = number_elseifs(elseifs, next, [])
{else_block, next} = number_optional(else_block, next)

{%{
node
| condition: condition,
then_block: then_block,
elseifs: elseifs,
else_block: else_block,
meta: with_id(node.meta, next)
}, next + 1}
end

defp number(%Statement.While{condition: condition, body: body} = node, next) do
{condition, next} = number(condition, next)
{body, next} = number(body, next)
{%{node | condition: condition, body: body, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.Repeat{body: body, condition: condition} = node, next) do
{body, next} = number(body, next)
{condition, next} = number(condition, next)
{%{node | body: body, condition: condition, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.ForNum{} = node, next) do
%Statement.ForNum{start: start, limit: limit, step: step, body: body} = node

{start, next} = number(start, next)
{limit, next} = number(limit, next)
{step, next} = number_optional(step, next)
{body, next} = number(body, next)

{%{node | start: start, limit: limit, step: step, body: body, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.ForIn{iterators: iterators, body: body} = node, next) do
{iterators, next} = number_list(iterators, next, [])
{body, next} = number(body, next)
{%{node | iterators: iterators, body: body, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.Do{body: body} = node, next) do
{body, next} = number(body, next)
{%{node | body: body, meta: with_id(node.meta, next)}, next + 1}
end

defp number(%Statement.Return{values: values} = node, next) do
{values, next} = number_list(values, next, [])
{%{node | values: values, meta: with_id(node.meta, next)}, next + 1}
end

# Every node shape must have a clause above. Silently skipping one would
# leave its whole subtree unnumbered, and the compiler's raw-term fallback
# key collides for structurally identical subtrees — a miscompile, not a
# slowdown — so an unrecognized node fails loudly instead.
defp number(node, _next) do
raise ArgumentError,
"Lua.AST.Ids has no number/2 clause for node: #{inspect(node, limit: 5)}"
end

defp number_optional(nil, next), do: {nil, next}
defp number_optional(node, next), do: number(node, next)

defp number_list([], next, acc), do: {Enum.reverse(acc), next}

defp number_list([node | rest], next, acc) do
{node, next} = number(node, next)
number_list(rest, next, [node | acc])
end

defp number_table_fields([], next, acc), do: {Enum.reverse(acc), next}

defp number_table_fields([{:list, value} | rest], next, acc) do
{value, next} = number(value, next)
number_table_fields(rest, next, [{:list, value} | acc])
end

defp number_table_fields([{:record, key, value} | rest], next, acc) do
{key, next} = number(key, next)
{value, next} = number(value, next)
number_table_fields(rest, next, [{:record, key, value} | acc])
end

defp number_elseifs([], next, acc), do: {Enum.reverse(acc), next}

defp number_elseifs([{condition, block} | rest], next, acc) do
{condition, next} = number(condition, next)
{block, next} = number(block, next)
number_elseifs(rest, next, [{condition, block} | acc])
end

defp with_id(nil, id), do: %Meta{id: id}
defp with_id(meta, id), do: %{meta | id: id}
end
36 changes: 27 additions & 9 deletions lib/lua/ast/meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@ defmodule Lua.AST.Meta do
@type t :: %__MODULE__{
start: position() | nil,
end: position() | nil,
metadata: map()
metadata: map(),
id: non_neg_integer() | nil
}

defstruct start: nil, end: nil, metadata: %{}
@typedoc """
Chunk-unique identifier for the node carrying this metadata.

`Lua.AST.Ids.assign/1` stamps every node of a parsed chunk, including the
bodies of nested functions. Compiler passes that need a per-node table key
it by this integer instead of by the node term, so a lookup hashes one word
rather than a whole subtree. Hand-built AST nodes carry `nil` until
`Lua.Compiler.compile/2` stamps the chunk they belong to.
"""
@type id :: non_neg_integer() | nil

defstruct start: nil, end: nil, metadata: %{}, id: nil

@doc """
Creates a new Meta struct with start and end positions.
Expand All @@ -42,7 +54,8 @@ defmodule Lua.AST.Meta do
%Lua.AST.Meta{
start: %{line: 1, column: 1, byte_offset: 0},
end: %{line: 1, column: 5, byte_offset: 4},
metadata: %{}
metadata: %{},
id: nil
}
"""
@spec new(position() | nil, position() | nil, map()) :: t()
Expand All @@ -53,7 +66,9 @@ defmodule Lua.AST.Meta do
@doc """
Merges two Meta structs, taking the earliest start and latest end.

Useful when combining multiple nodes into a single parent node.
Useful when combining multiple nodes into a single parent node. The
`metadata` (comments) and `id` of the left operand are kept, so merging a
wider span into a node never silently drops what was already attached to it.

## Examples

Expand All @@ -63,14 +78,17 @@ defmodule Lua.AST.Meta do
%Lua.AST.Meta{
start: %{line: 1, column: 1, byte_offset: 0},
end: %{line: 1, column: 10, byte_offset: 9},
metadata: %{}
metadata: %{},
id: nil
}
"""
@spec merge(t(), t()) :: t()
def merge(%__MODULE__{start: start1, end: end1}, %__MODULE__{start: start2, end: end2}) do
new_start = earliest_position(start1, start2)
new_end = latest_position(end1, end2)
new(new_start, new_end)
def merge(%__MODULE__{} = left, %__MODULE__{} = right) do
%{
left
| start: earliest_position(left.start, right.start),
end: latest_position(left.end, right.end)
}
end

@doc """
Expand Down
9 changes: 9 additions & 0 deletions lib/lua/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Lua.Compiler do
"""

alias Lua.AST.Chunk
alias Lua.AST.Ids
alias Lua.Compiler.Bytecode
alias Lua.Compiler.Codegen
alias Lua.Compiler.GotoResolution
Expand All @@ -29,6 +30,14 @@ defmodule Lua.Compiler do
"""
@spec compile(Chunk.t(), compile_opts()) :: {:ok, Prototype.t()} | {:error, term()}
def compile(%Chunk{} = chunk, opts \\ []) do
# Scope resolution and codegen key per-node tables by `meta.id` (see
# `Lua.AST.Ids`); without ids, structurally identical nodes (e.g. two
# empty loop bodies) would share one table entry and miscompile. Stamping
# here covers chunks that never went through the parser, such as those
# built with `Lua.AST.Builder`. Assignment is deterministic, so a parsed
# chunk (already stamped by `Lua.Parser`) re-stamps to the same ids.
chunk = Ids.assign(chunk)

with :ok <- GotoValidation.validate(chunk),
{:ok, scope_state} <- Scope.resolve(chunk, opts),
{:ok, prototype} <- Codegen.generate(chunk, scope_state, opts) do
Expand Down
Loading
Loading