Skip to content

Commit 430dc9b

Browse files
committed
Initial commit for implementing code blocks.
1 parent 7b5dd34 commit 430dc9b

File tree

10 files changed

+74
-14
lines changed

10 files changed

+74
-14
lines changed

spec/parsers/function_spec.cr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ describe "Function Definition" do
99

1010
expect_error "fun", Mint::Parser::FunctionExpectedName
1111
expect_error "fun ", Mint::Parser::FunctionExpectedName
12-
expect_error "fun a", Mint::Parser::FunctionExpectedOpeningBracket
13-
expect_error "fun a ", Mint::Parser::FunctionExpectedOpeningBracket
12+
expect_error "fun a", Mint::SyntaxError
13+
expect_error "fun a ", Mint::SyntaxError
1414
expect_error "fun a (", Mint::Parser::FunctionExpectedClosingParentheses
1515
expect_error "fun a ( ", Mint::Parser::FunctionExpectedClosingParentheses
16-
expect_error "fun a ()", Mint::Parser::FunctionExpectedOpeningBracket
17-
expect_error "fun a () ", Mint::Parser::FunctionExpectedOpeningBracket
16+
expect_error "fun a ()", Mint::SyntaxError
17+
expect_error "fun a () ", Mint::SyntaxError
1818
expect_error "fun a () :", Mint::Parser::FunctionExpectedTypeOrVariable
1919
expect_error "fun a () : ", Mint::Parser::FunctionExpectedTypeOrVariable
20+
expect_error "fun a () { x", Mint::SyntaxError
2021

2122
expect_ok "fun a { true } "
2223
expect_ok "fun a : T { true } "

src/ast/function.cr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
module Mint
22
class Ast
3+
class Block < Node
4+
getter statements, expression, tail_comments
5+
6+
def initialize(@tail_comments : Array(Comment),
7+
@statements : Array(Node),
8+
@expression : Expression,
9+
@input : Data,
10+
@from : Int32,
11+
@to : Int32)
12+
end
13+
end
14+
315
class Function < Node
416
getter name, where, arguments, body, type
517
getter comment, head_comments, tail_comments

src/ast/statement.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Mint
55
Try
66
Sequence
77
Parallel
8+
None
89
end
910

1011
getter target, expression, parent

src/compilers/function.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ module Mint
88
end
99
end
1010

11+
def _compile(node : Ast::Block) : String
12+
compile node.expression
13+
end
14+
1115
def _compile(node : Ast::Function, contents = "") : String
1216
name =
1317
js.variable_of(node)

src/formatters/function.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module Mint
22
class Formatter
3+
def format(node : Ast::Block) : String
4+
list node.statements + [node.expression] + node.tail_comments
5+
end
6+
37
def format(node : Ast::Function) : String
48
name =
59
format node.name

src/parsers/block.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module Mint
2121
whitespace
2222

2323
result = yield
24-
2524
whitespace
25+
2626
char '}', closing_bracket
2727
result
2828
end

src/parsers/function.cr

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@ module Mint
77
syntax_error FunctionExpectedExpression
88
syntax_error FunctionExpectedName
99

10+
def code_block : Ast::Block?
11+
start do |start_position|
12+
char '{', SyntaxError
13+
whitespace
14+
15+
statements =
16+
many { comment || statement(:none, require_name: true) }
17+
18+
whitespace
19+
20+
expression =
21+
expression! SyntaxError
22+
23+
whitespace
24+
tail_comments = many { comment }
25+
26+
whitespace
27+
char '}', SyntaxError
28+
29+
self << Ast::Block.new(
30+
tail_comments: tail_comments,
31+
statements: statements,
32+
expression: expression,
33+
from: start_position,
34+
to: position,
35+
input: data)
36+
end
37+
end
38+
1039
def function : Ast::Function?
1140
start do |start_position|
1241
comment = self.comment
@@ -41,21 +70,24 @@ module Mint
4170
item
4271
end
4372

44-
head_comments, body, tail_comments = block_with_comments(
45-
opening_bracket: FunctionExpectedOpeningBracket,
46-
closing_bracket: FunctionExpectedClosingBracket
47-
) do
48-
expression! FunctionExpectedExpression
49-
end
73+
body =
74+
code_block
75+
76+
# head_comments, body, tail_comments = block_with_comments(
77+
# opening_bracket: FunctionExpectedOpeningBracket,
78+
# closing_bracket: FunctionExpectedClosingBracket
79+
# ) do
80+
# expression! FunctionExpectedExpression
81+
# end
5082

5183
end_position = position
5284

5385
whitespace
5486

5587
self << Ast::Function.new(
88+
head_comments: [] of Ast::Comment,
89+
tail_comments: [] of Ast::Comment,
5690
body: body.as(Ast::Expression),
57-
head_comments: head_comments,
58-
tail_comments: tail_comments,
5991
arguments: arguments,
6092
from: start_position,
6193
comment: comment,

src/parsers/statement.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Mint
22
class Parser
3-
def statement(parent : Ast::Statement::Parent) : Ast::Statement?
3+
def statement(parent : Ast::Statement::Parent, require_name : Bool = false) : Ast::Statement?
44
start do |start_position|
55
target = start do
66
value = variable(track: false) || tuple_destructuring
@@ -11,6 +11,7 @@ module Mint
1111
value
1212
end
1313

14+
next if require_name && !target
1415
body = expression
1516

1617
next unless body

src/parsers/tuple_destructuring.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Mint
88
next unless char! '{'
99
value = tuple_destructuring || variable
1010
whitespace
11+
next if char.in?('|', '=') # Don't parse record or record update as tuple destructuring
1112
char! ','
1213
whitespace
1314
value

src/type_checkers/function.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ module Mint
1717
Comparer.normalize(defined_type)
1818
end
1919

20+
def check(node : Ast::Block) : Checkable
21+
resolve node.expression
22+
end
23+
2024
def check(node : Ast::Function) : Checkable
2125
scope node do
2226
scope node.where.try(&.statements) || [] of Ast::WhereStatement do

0 commit comments

Comments
 (0)