Skip to content

Commit

Permalink
support parse return
Browse files Browse the repository at this point in the history
  • Loading branch information
cocolato committed Oct 30, 2024
1 parent 61621d5 commit 2e50d1f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
13 changes: 2 additions & 11 deletions lib/emolang/ast/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@
module Emolang
module AST
class Statement < Node
attr_accessor :token

def statement_name
raise NotImplementedError
end
end

class LetStatement < Statement
attr_accessor :token, :name

sig { params(token: Token).void }
def initialize(token)
super()
@token = token
@name = T.let(nil, T.nilable(AST::Identifier))
end

sig { returns(String) }
def token_literal
Expand Down
28 changes: 28 additions & 0 deletions lib/emolang/ast/statement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true
# typed: true

module Emolang
module AST
class LetStatement < Statement
attr_accessor :token, :name

sig { params(token: Token).void }
def initialize(token)
super()
@token = token
@name = T.let(nil, T.nilable(AST::Identifier))
end
end

class ReturnStatement < Statement
attr_accessor :token, :return_value

sig { params(token: Token).void }
def initialize(token)
super()
@token = token
@name = T.let(nil, T.nilable(AST::Identifier))
end
end
end
end
16 changes: 15 additions & 1 deletion lib/emolang/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,24 @@ def parse_statement

when TokenTypeEnum::LET
parse_let_statement
when TokenTypeEnum::RETURN
parse_return_statement
end
end

sig { returns(T.nilable(AST::Statement)) }
sig { returns(T.nilable(AST::ReturnStatement)) }
def parse_return_statement
stmt = AST::ReturnStatement.new(@cur_token)
next_token
loop do
break if @cur_token.type == TokenTypeEnum::SEMICOLON

next_token
end
stmt
end

sig { returns(T.nilable(AST::LetStatement)) }
def parse_let_statement
stmt = AST::LetStatement.new(@cur_token)

Expand Down
8 changes: 8 additions & 0 deletions test/test_emolang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ def test_next_token
assert_equal(test[1], token.literal)
end
end

def test_parse_let
input = 'let a = 1;'
lexer = Emolang::Lexer.new(input)
parser = Emolang::EParser.new(lexer)
parser.parse_program
assert_equal(parser.errors, [])
end
end

0 comments on commit 2e50d1f

Please sign in to comment.