Skip to content

Commit

Permalink
Merge pull request #2 from ryokosuge/feature/chapter-2
Browse files Browse the repository at this point in the history
2章 構文解析
  • Loading branch information
ryokosuge authored Aug 17, 2018
2 parents 158e838 + 97a8d05 commit 2225190
Show file tree
Hide file tree
Showing 9 changed files with 1,551 additions and 4 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,28 @@
## 1章 字句解析

- release: [chapter1](https://github.com/ryokosuge/go-monkey/releases/tag/chapter-1)
- pr: [#1](https://github.com/ryokosuge/go-monkey/pull/1)
- pr: [#1](https://github.com/ryokosuge/go-monkey/pull/1)

## 2章 構文解析

- release: [chapter2](https://github.com/ryokosuge/go-monkey/releases/tag/chapter-2)
- pr: [#2](https://github.com/ryokosuge/go-monkey/pull/2)

- [2.4 構文解析器の第一歩 : let文](https://github.com/ryokosuge/go-monkey/pull/4)
- [2.5 return文の構文解析](https://github.com/ryokosuge/go-monkey/pull/5)
- [2.6 式の構文解析](https://github.com/ryokosuge/go-monkey/pull/6)
- [2.6.4 ASTの準備](https://github.com/ryokosuge/go-monkey/pull/8)
- [2.6.5 Pratt構文解析器の実装](https://github.com/ryokosuge/go-monkey/pull/9)
- [2.6.6 識別子](https://github.com/ryokosuge/go-monkey/pull/10)
- [2.6.7 整数リテラル](https://github.com/ryokosuge/go-monkey/pull/11)
- [2.6.8 前置演算子](https://github.com/ryokosuge/go-monkey/pull/12)
- [2.6.9 中置演算子](https://github.com/ryokosuge/go-monkey/pull/13)
- [2.7 Pratt構文解析の仕組み](https://github.com/ryokosuge/go-monkey/pull/14)
- [2.8 構文解析器の拡張](https://github.com/ryokosuge/go-monkey/pull/15)
- [2.8.1 真偽値リテラル](https://github.com/ryokosuge/go-monkey/pull/16)
- [2.8.2 グループ化された式](https://github.com/ryokosuge/go-monkey/pull/17)
- [2.8.3 if式](https://github.com/ryokosuge/go-monkey/pull/18)
- [2.8.4 関数リテラル](https://github.com/ryokosuge/go-monkey/pull/19)
- [2.8.5 呼び出し式](https://github.com/ryokosuge/go-monkey/pull/20)
- [2.8.6 TODOの削除](https://github.com/ryokosuge/go-monkey/pull/21)
- [2.9 読み込み - 構文解析 - 表示 - 繰り返し](https://github.com/ryokosuge/go-monkey/pull/22)
Binary file added pkg/darwin_amd64/monkey/ast.a
Binary file not shown.
Binary file added pkg/darwin_amd64/monkey/lexer.a
Binary file not shown.
287 changes: 287 additions & 0 deletions src/monkey/ast/ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
package ast

import (
"bytes"
"monkey/token"
"strings"
)

type Node interface {
TokenLiteral() string
String() string
}

type Statement interface {
Node
statementNode()
}

type Expression interface {
Node
expressionNode()
}

type Program struct {
Statements []Statement
}

func (p *Program) TokenLiteral() string {
if len(p.Statements) > 0 {
return p.Statements[0].TokenLiteral()
} else {
return ""
}
}

func (p *Program) String() string {
var out bytes.Buffer

for _, s := range p.Statements {
out.WriteString(s.String())
}
return out.String()
}

type Identifier struct {
Token token.Token
Value string
}

func (i *Identifier) expressionNode() {}
func (i *Identifier) TokenLiteral() string {
return i.Token.Literal
}

func (i *Identifier) String() string {
return i.Value
}

type LetStatement struct {
Token token.Token
Name *Identifier
Value Expression
}

func (ls *LetStatement) statementNode() {}
func (ls *LetStatement) TokenLiteral() string {
return ls.Token.Literal
}

func (ls *LetStatement) String() string {
var out bytes.Buffer

out.WriteString(ls.TokenLiteral() + " ")
out.WriteString(ls.Name.String())
out.WriteString(" = ")

if ls.Value != nil {
out.WriteString(ls.Value.String())
}

out.WriteString(";")
return out.String()
}

type ReturnStatement struct {
Token token.Token // 'return'トークン
ReturnValue Expression
}

func (rs *ReturnStatement) statementNode() {}
func (rs *ReturnStatement) TokenLiteral() string {
return rs.Token.Literal
}

func (rs *ReturnStatement) String() string {
var out bytes.Buffer

out.WriteString(rs.TokenLiteral() + " ")
if rs.ReturnValue != nil {
out.WriteString(rs.ReturnValue.String())
}

out.WriteString(";")
return out.String()
}

type ExpressionStatement struct {
Token token.Token
Expression Expression
}

func (es *ExpressionStatement) statementNode() {}
func (es *ExpressionStatement) TokenLiteral() string {
return es.Token.Literal
}

func (es *ExpressionStatement) String() string {
if es.Expression != nil {
return es.Expression.String()
}
return ""
}

type IntegerLiteral struct {
Token token.Token
Value int64
}

func (il *IntegerLiteral) expressionNode() {}
func (il *IntegerLiteral) TokenLiteral() string {
return il.Token.Literal
}
func (il *IntegerLiteral) String() string {
return il.Token.Literal
}

type PrefixExpression struct {
Token token.Token
Operator string
Right Expression
}

func (pe *PrefixExpression) expressionNode() {}
func (pe *PrefixExpression) TokenLiteral() string {
return pe.Token.Literal
}
func (pe *PrefixExpression) String() string {
var out bytes.Buffer
out.WriteString("(")
out.WriteString(pe.Operator)
out.WriteString(pe.Right.String())
out.WriteString(")")

return out.String()
}

type InfixExpression struct {
Token token.Token
Left Expression
Operator string
Right Expression
}

func (oe *InfixExpression) expressionNode() {}
func (oe *InfixExpression) TokenLiteral() string {
return oe.Token.Literal
}
func (oe *InfixExpression) String() string {
var out bytes.Buffer

out.WriteString("(")
out.WriteString(oe.Left.String())
out.WriteString(" " + oe.Operator + " ")
out.WriteString(oe.Right.String())
out.WriteString(")")

return out.String()
}

type Boolean struct {
Token token.Token
Value bool
}

func (b *Boolean) expressionNode() {}
func (b *Boolean) TokenLiteral() string {
return b.Token.Literal
}
func (b *Boolean) String() string {
return b.Token.Literal
}

type IfExpression struct {
Token token.Token
Condition Expression
Consequence *BlockStatement
Alternative *BlockStatement
}

func (ie *IfExpression) expressionNode() {}
func (ie *IfExpression) TokenLiteral() string {
return ie.Token.Literal
}
func (ie *IfExpression) String() string {
var out bytes.Buffer
out.WriteString("if")
out.WriteString(ie.Condition.String())
out.WriteString(" ")
out.WriteString(ie.Consequence.String())

if ie.Alternative != nil {
out.WriteString("else ")
out.WriteString(ie.Alternative.String())
}

return out.String()
}

type BlockStatement struct {
Token token.Token
Statements []Statement
}

func (bs *BlockStatement) statementNode() {}
func (bs *BlockStatement) TokenLiteral() string {
return bs.Token.Literal
}
func (bs *BlockStatement) String() string {
var out bytes.Buffer
for _, s := range bs.Statements {
out.WriteString(s.String())
}
return out.String()
}

type FunctionLiteral struct {
Token token.Token // 'fn' トークン
Parameters []*Identifier
Body *BlockStatement
}

func (fl *FunctionLiteral) expressionNode() {}
func (fl *FunctionLiteral) TokenLiteral() string {
return fl.Token.Literal
}
func (fl *FunctionLiteral) String() string {
var out bytes.Buffer

params := []string{}
for _, p := range fl.Parameters {
params = append(params, p.String())
}

out.WriteString(fl.TokenLiteral())
out.WriteString("(")
out.WriteString(strings.Join(params, ", "))
out.WriteString(") ")
out.WriteString(fl.Body.String())

return out.String()
}

type CallExpression struct {
Token token.Token // '(' トークン
Function Expression // Identifier または FunctionLiteral
Arguments []Expression
}

func (ce *CallExpression) expressionNode() {}
func (ce *CallExpression) TokenLiteral() string {
return ce.Token.Literal
}
func (ce *CallExpression) String() string {
var out bytes.Buffer

args := []string{}
for _, a := range ce.Arguments {
args = append(args, a.String())
}

out.WriteString(ce.Function.String())
out.WriteString("(")
out.WriteString(strings.Join(args, ", "))
out.WriteString(")")

return out.String()
}
28 changes: 28 additions & 0 deletions src/monkey/ast/ast_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ast

import (
"monkey/token"
"testing"
)

func TestString(t *testing.T) {
program := &Program{
Statements: []Statement{
&LetStatement{
Token: token.Token{Type: token.LET, Literal: "let"},
Name: &Identifier{
Token: token.Token{Type: token.IDENT, Literal: "myVar"},
Value: "myVar",
},
Value: &Identifier{
Token: token.Token{Type: token.IDENT, Literal: "anotherVar"},
Value: "anotherVar",
},
},
},
}

if program.String() != "let myVar = anotherVar;" {
t.Errorf("program.String() wrong. got=%q", program.String())
}
}
Loading

0 comments on commit 2225190

Please sign in to comment.