grongigo 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/MIT +21 -0
  3. data/README.md +266 -0
  4. data/Rakefile +8 -0
  5. data/bin/grongigo +157 -0
  6. data/examples/calc.grg +17 -0
  7. data/examples/factorial.grg +21 -0
  8. data/examples/fizzbuzz.grg +86 -0
  9. data/examples/hello.grg +5 -0
  10. data/grongigo.gemspec +30 -0
  11. data/lib/grongigo/ast/assign_expr.rb +18 -0
  12. data/lib/grongigo/ast/binary_expr.rb +19 -0
  13. data/lib/grongigo/ast/block_stmt.rb +17 -0
  14. data/lib/grongigo/ast/break_stmt.rb +14 -0
  15. data/lib/grongigo/ast/call_expr.rb +18 -0
  16. data/lib/grongigo/ast/case_clause.rb +18 -0
  17. data/lib/grongigo/ast/char_literal.rb +17 -0
  18. data/lib/grongigo/ast/continue_stmt.rb +14 -0
  19. data/lib/grongigo/ast/expr_stmt.rb +17 -0
  20. data/lib/grongigo/ast/for_stmt.rb +20 -0
  21. data/lib/grongigo/ast/function_decl.rb +20 -0
  22. data/lib/grongigo/ast/identifier.rb +17 -0
  23. data/lib/grongigo/ast/if_stmt.rb +19 -0
  24. data/lib/grongigo/ast/index_expr.rb +18 -0
  25. data/lib/grongigo/ast/node.rb +15 -0
  26. data/lib/grongigo/ast/number_literal.rb +17 -0
  27. data/lib/grongigo/ast/parameter.rb +18 -0
  28. data/lib/grongigo/ast/program.rb +17 -0
  29. data/lib/grongigo/ast/return_stmt.rb +17 -0
  30. data/lib/grongigo/ast/string_literal.rb +17 -0
  31. data/lib/grongigo/ast/switch_stmt.rb +19 -0
  32. data/lib/grongigo/ast/unary_expr.rb +19 -0
  33. data/lib/grongigo/ast/var_decl.rb +19 -0
  34. data/lib/grongigo/ast/while_stmt.rb +18 -0
  35. data/lib/grongigo/ast.rb +30 -0
  36. data/lib/grongigo/codegen.rb +357 -0
  37. data/lib/grongigo/compiler.rb +156 -0
  38. data/lib/grongigo/constants.rb +129 -0
  39. data/lib/grongigo/jp2grg.rb +117 -0
  40. data/lib/grongigo/lexer.rb +349 -0
  41. data/lib/grongigo/parse_error.rb +13 -0
  42. data/lib/grongigo/parser.rb +572 -0
  43. data/lib/grongigo/token.rb +23 -0
  44. data/lib/grongigo.rb +12 -0
  45. metadata +90 -0
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Function call
8
+ class CallExpr < Node
9
+ attr_reader :callee, :arguments
10
+
11
+ def initialize(callee, arguments, line = 0, column = 0)
12
+ super(line, column)
13
+ @callee = callee
14
+ @arguments = arguments
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # case clause
8
+ class CaseClause < Node
9
+ attr_reader :value, :statements
10
+
11
+ def initialize(value, statements, line = 0, column = 0)
12
+ super(line, column)
13
+ @value = value
14
+ @statements = statements
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Character literal
8
+ class CharLiteral < Node
9
+ attr_reader :value
10
+
11
+ def initialize(value, line = 0, column = 0)
12
+ super(line, column)
13
+ @value = value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # continue statement
8
+ class ContinueStmt < Node
9
+ def initialize(line = 0, column = 0)
10
+ super(line, column)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Expression statement
8
+ class ExprStmt < Node
9
+ attr_reader :expression
10
+
11
+ def initialize(expression, line = 0, column = 0)
12
+ super(line, column)
13
+ @expression = expression
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # for statement
8
+ class ForStmt < Node
9
+ attr_reader :init, :condition, :update, :body
10
+
11
+ def initialize(init, condition, update, body, line = 0, column = 0)
12
+ super(line, column)
13
+ @init = init
14
+ @condition = condition
15
+ @update = update
16
+ @body = body
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # 関数定義
8
+ class FunctionDecl < Node
9
+ attr_reader :return_type, :name, :params, :body
10
+
11
+ def initialize(return_type, name, params, body, line = 0, column = 0)
12
+ super(line, column)
13
+ @return_type = return_type
14
+ @name = name
15
+ @params = params
16
+ @body = body
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Identifier
8
+ class Identifier < Node
9
+ attr_reader :name
10
+
11
+ def initialize(name, line = 0, column = 0)
12
+ super(line, column)
13
+ @name = name
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # if statement
8
+ class IfStmt < Node
9
+ attr_reader :condition, :then_branch, :else_branch
10
+
11
+ def initialize(condition, then_branch, else_branch = nil, line = 0, column = 0)
12
+ super(line, column)
13
+ @condition = condition
14
+ @then_branch = then_branch
15
+ @else_branch = else_branch
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Array access
8
+ class IndexExpr < Node
9
+ attr_reader :array, :index
10
+
11
+ def initialize(array, index, line = 0, column = 0)
12
+ super(line, column)
13
+ @array = array
14
+ @index = index
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Grongigo
4
+ module AST
5
+ # AST Node base class
6
+ class Node
7
+ attr_reader :line, :column
8
+
9
+ def initialize(line = 0, column = 0)
10
+ @line = line
11
+ @column = column
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Number literal
8
+ class NumberLiteral < Node
9
+ attr_reader :value
10
+
11
+ def initialize(value, line = 0, column = 0)
12
+ super(line, column)
13
+ @value = value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Parameters
8
+ class Parameter < Node
9
+ attr_reader :type, :name
10
+
11
+ def initialize(type, name, line = 0, column = 0)
12
+ super(line, column)
13
+ @type = type
14
+ @name = name
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # プログラム全体
8
+ class Program < Node
9
+ attr_reader :declarations
10
+
11
+ def initialize(declarations)
12
+ super(0, 0)
13
+ @declarations = declarations
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # return statement
8
+ class ReturnStmt < Node
9
+ attr_reader :value
10
+
11
+ def initialize(value = nil, line = 0, column = 0)
12
+ super(line, column)
13
+ @value = value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # String literal
8
+ class StringLiteral < Node
9
+ attr_reader :value
10
+
11
+ def initialize(value, line = 0, column = 0)
12
+ super(line, column)
13
+ @value = value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # switch statement
8
+ class SwitchStmt < Node
9
+ attr_reader :expression, :cases, :default_case
10
+
11
+ def initialize(expression, cases, default_case = nil, line = 0, column = 0)
12
+ super(line, column)
13
+ @expression = expression
14
+ @cases = cases
15
+ @default_case = default_case
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Unary expression
8
+ class UnaryExpr < Node
9
+ attr_reader :operator, :operand, :prefix
10
+
11
+ def initialize(operator, operand, prefix = true, line = 0, column = 0)
12
+ super(line, column)
13
+ @operator = operator
14
+ @operand = operand
15
+ @prefix = prefix
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # Variable declaration
8
+ class VarDecl < Node
9
+ attr_reader :type, :name, :initializer
10
+
11
+ def initialize(type, name, initializer = nil, line = 0, column = 0)
12
+ super(line, column)
13
+ @type = type
14
+ @name = name
15
+ @initializer = initializer
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Grongigo
6
+ module AST
7
+ # while statement
8
+ class WhileStmt < Node
9
+ attr_reader :condition, :body
10
+
11
+ def initialize(condition, body, line = 0, column = 0)
12
+ super(line, column)
13
+ @condition = condition
14
+ @body = body
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ast/node'
4
+ require_relative 'ast/program'
5
+ require_relative 'ast/function_decl'
6
+ require_relative 'ast/parameter'
7
+ require_relative 'ast/var_decl'
8
+
9
+ # Statements
10
+ require_relative 'ast/block_stmt'
11
+ require_relative 'ast/if_stmt'
12
+ require_relative 'ast/while_stmt'
13
+ require_relative 'ast/for_stmt'
14
+ require_relative 'ast/return_stmt'
15
+ require_relative 'ast/break_stmt'
16
+ require_relative 'ast/continue_stmt'
17
+ require_relative 'ast/expr_stmt'
18
+ require_relative 'ast/switch_stmt'
19
+ require_relative 'ast/case_clause'
20
+
21
+ # Expressions
22
+ require_relative 'ast/binary_expr'
23
+ require_relative 'ast/unary_expr'
24
+ require_relative 'ast/assign_expr'
25
+ require_relative 'ast/call_expr'
26
+ require_relative 'ast/index_expr'
27
+ require_relative 'ast/identifier'
28
+ require_relative 'ast/number_literal'
29
+ require_relative 'ast/string_literal'
30
+ require_relative 'ast/char_literal'