Skip to content

Commit

Permalink
Convert type_id from String to a node (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
jansul authored May 2, 2023
1 parent 1590d81 commit 3f4cbcf
Show file tree
Hide file tree
Showing 84 changed files with 270 additions and 230 deletions.
10 changes: 6 additions & 4 deletions src/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module Mint
end

def main : Component?
@components.find(&.name.==("Main"))
@components.find(&.name.value.==("Main"))
end

def self.space_separated?(node1, node2)
Expand Down Expand Up @@ -92,13 +92,15 @@ module Mint
def normalize
@unified_modules =
@modules
.group_by(&.name)
.map do |name, modules|
.group_by(&.name.value)
.map do |_, modules|
Module.new(
functions: modules.flat_map(&.functions),
constants: modules.flat_map(&.constants),
input: Data.new(input: "", file: ""),
name: name,
# TODO: We may need to store each modules name node for
# future features, but for now we just store the first
name: modules.first.name,
comments: [] of Comment,
comment: nil,
from: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/ast/component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Mint
@gets : Array(Get),
@uses : Array(Use),
@global : Bool,
@name : String,
@name : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/connect.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mint
getter keys, store

def initialize(@keys : Array(ConnectVariable),
@store : String,
@store : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/directives/documentation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mint
class Documentation < Node
getter entity

def initialize(@entity : String,
def initialize(@entity : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/enum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Mint
@options : Array(EnumOption),
@comments : Array(Comment),
@comment : Comment?,
@name : String,
@name : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
4 changes: 2 additions & 2 deletions src/ast/enum_destructuring.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Mint
getter name, option, parameters

def initialize(@parameters : Array(Node),
@option : String,
@name : String?,
@option : TypeId,
@name : TypeId?,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
4 changes: 2 additions & 2 deletions src/ast/enum_id.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Mint
getter option, name, expressions

def initialize(@expressions : Array(Expression),
@option : String,
@name : String?,
@option : TypeId,
@name : TypeId?,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/enum_option.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Mint

def initialize(@parameters : Array(Node),
@comment : Comment?,
@value : String,
@value : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/html_component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Mint
@comments : Array(Comment),
@children : Array(Node),
@ref : Variable?,
@component : Variable,
@component : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/module.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Mint
@constants : Array(Constant),
@comments : Array(Comment),
@comment : Comment?,
@name : String,
@name : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/module_access.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Mint
getter? constant

def initialize(@variable : Variable,
@name : String,
@name : TypeId,
@from : Int32,
@input : Data,
@to : Int32,
Expand Down
4 changes: 2 additions & 2 deletions src/ast/provider.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module Mint
@constants : Array(Constant),
@comments : Array(Comment),
@states : Array(State),
@subscription : String,
@subscription : TypeId,
@comment : Comment?,
@gets : Array(Get),
@name : String,
@name : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/record_definition.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint
def initialize(@fields : Array(RecordDefinitionField),
@block_comment : Comment?,
@comment : Comment?,
@name : String,
@name : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Mint
@states : Array(State),
@comment : Comment?,
@gets : Array(Get),
@name : String,
@name : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mint
getter name, parameters

def initialize(@parameters : Array(TypeOrVariable),
@name : String,
@name : TypeId,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
13 changes: 13 additions & 0 deletions src/ast/type_id.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Mint
class Ast
class TypeId < Node
getter value

def initialize(@value : String,
@input : Data,
@from : Int32,
@to : Int32)
end
end
end
end
2 changes: 1 addition & 1 deletion src/ast/use.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mint
getter data, provider, condition

def initialize(@condition : Expression?,
@provider : String,
@provider : TypeId,
@data : Record,
@input : Data,
@from : Int32,
Expand Down
4 changes: 2 additions & 2 deletions src/compilers/component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module Mint

def compile_component_store_data(node : Ast::Component) : Array(String)
node.connects.reduce(%w[]) do |memo, item|
store = ast.stores.find(&.name.==(item.store))
store = ast.stores.find(&.name.value.==(item.store.value))

if store
item.keys.map do |key|
Expand Down Expand Up @@ -145,7 +145,7 @@ module Mint

node.connects.each do |item|
store =
ast.stores.find(&.name.==(item.store))
ast.stores.find(&.name.value.==(item.store.value))

if store
name =
Expand Down
2 changes: 1 addition & 1 deletion src/compilers/style.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Mint
end

def _compile(node : Ast::Style, component : Ast::Component) : Nil
style_builder.process(node, component.name.gsub('.', '·'))
style_builder.process(node, component.name.value.gsub('.', '·'))
end
end
end
20 changes: 10 additions & 10 deletions src/compilers/top_level.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Mint
new(artifacts, **options)

main =
compiler.ast.components.find(&.name.==("Main")).try do |component|
compiler.ast.components.find(&.name.value.==("Main")).try do |component|
globals =
compiler
.ast
Expand Down Expand Up @@ -55,7 +55,7 @@ module Mint
new(artifacts, **options)

main =
compiler.ast.components.find(&.name.==("Main")).try do |component|
compiler.ast.components.find(&.name.value.==("Main")).try do |component|
globals =
compiler
.ast
Expand Down Expand Up @@ -147,47 +147,47 @@ module Mint
# --------------------------------------------------------------------------

def maybe
ast.enums.find!(&.name.==("Maybe"))
ast.enums.find!(&.name.value.==("Maybe"))
end

def just
node =
maybe.options.find!(&.value.==("Just"))
maybe.options.find!(&.value.value.==("Just"))

js.class_of(node)
end

def nothing
node =
maybe.options.find!(&.value.==("Nothing"))
maybe.options.find!(&.value.value.==("Nothing"))

js.class_of(node)
end

# --------------------------------------------------------------------------

def result
ast.enums.find!(&.name.==("Result"))
ast.enums.find!(&.name.value.==("Result"))
end

def ok
node =
result.options.find!(&.value.==("Ok"))
result.options.find!(&.value.value.==("Ok"))

js.class_of(node)
end

def err
node =
result.options.find!(&.value.==("Err"))
result.options.find!(&.value.value.==("Err"))

js.class_of(node)
end

def compiled_web_components
@web_components.compact_map do |component, tagname|
node =
ast.components.find(&.name.==(component))
ast.components.find(&.name.value.==(component))

next unless node

Expand All @@ -213,7 +213,7 @@ module Mint
# Wraps the application with the runtime
def wrap_runtime(body, main = "")
html_event_module =
ast.unified_modules.find!(&.name.==("Html.Event"))
ast.unified_modules.find!(&.name.value.==("Html.Event"))

from_event =
html_event_module.functions.find!(&.name.value.==("fromEvent"))
Expand Down
2 changes: 1 addition & 1 deletion src/compilers/variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Mint
case parent
when Ast::Component
parent.connects.each do |connect|
store = ast.stores.find(&.name.==(connect.store))
store = ast.stores.find(&.name.value.==(connect.store.value))

name =
case entity
Expand Down
12 changes: 6 additions & 6 deletions src/documentation_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,27 @@ module Mint
end

json.field "components" do
generate ast.components.sort_by(&.name), json
generate ast.components.sort_by(&.name.value), json
end

json.field "stores" do
generate ast.stores.sort_by(&.name), json
generate ast.stores.sort_by(&.name.value), json
end

json.field "modules" do
generate ast.unified_modules.sort_by(&.name), json
generate ast.unified_modules.sort_by(&.name.value), json
end

json.field "providers" do
generate ast.providers.sort_by(&.name), json
generate ast.providers.sort_by(&.name.value), json
end

json.field "records" do
generate ast.records.sort_by(&.name), json
generate ast.records.sort_by(&.name.value), json
end

json.field "enums" do
generate ast.enums.sort_by(&.name), json
generate ast.enums.sort_by(&.name.value), json
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/documentation_generator/component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Mint
def generate(node : Ast::Component, json : JSON::Builder)
json.object do
json.field "description", node.comment.try(&.to_html)
json.field "name", node.name
json.field "name" do
generate node.name, json
end

json.field "connects" do
generate node.connects, json
Expand Down
4 changes: 3 additions & 1 deletion src/documentation_generator/connect.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Mint
def generate(node : Ast::Connect, json : JSON::Builder)
json.object do
json.field "keys", node.keys.map(&.variable.value)
json.field "store", node.store
json.field "store" do
generate node.store, json
end
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/documentation_generator/enum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Mint
def generate(node : Ast::Enum, json : JSON::Builder)
json.object do
json.field "description", node.comment.try(&.to_html)
json.field "name", node.name
json.field "name" do
generate node.name, json
end

json.field "parameters" do
generate node.parameters, json
Expand Down
4 changes: 3 additions & 1 deletion src/documentation_generator/enum_option.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Mint
def generate(node : Ast::EnumOption, json : JSON::Builder)
json.object do
json.field "description", node.comment.try(&.to_html)
json.field "name", node.value
json.field "name" do
generate node.value, json
end

json.field "parameters" do
generate node.parameters, json
Expand Down
4 changes: 3 additions & 1 deletion src/documentation_generator/module.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Mint
def generate(node : Ast::Module, json : JSON::Builder)
json.object do
json.field "description", node.comment.try(&.to_html)
json.field "name", node.name
json.field "name" do
generate node.name, json
end

json.field "functions" do
generate node.functions, json
Expand Down
Loading

0 comments on commit 3f4cbcf

Please sign in to comment.