SlideShare a Scribd company logo
1 of 41
Download to read offline
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
歯磨き.go #2
構文解析
歯磨き.goとは
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
一過性の勉強会ではなく、少しずつ無理のない範囲で
毎日の歯磨きのように続けていくことを目的とした
ゆるふわなgo勉強会です。
(もともと社内でやってたけどスピンオフしました)
歯磨き.goとは
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
・社内で静的解析の勉強会を始めたので
 そのアウトプットの場になれば。会が進んできたら、 社内で
作った静的解析ツールを紹介(自慢)する場に
 なれば良いなとか
・社内だけだと閉塞感でると思うので社外の方用のLT
 枠も用意するのでぜひぜひ登壇したい方大歓迎。
 ゆるふわな会なので怖く無いよ。
自己紹介
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
自己紹介
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
山下 大輔 Daisuke Yamashita
ものづくり系エンジニア

go言語に限らずものづくりに関わる開発は
全般的に好き。



毎日使う最強のツールを作るのが目標。

目次
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
目次
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
・ANDPADの社内勉強会
・コンパイラの世界の構文解析
・静的解析の世界の構文解析


・静的解析を使って構文解析をして四則演算機を作る
ANDPADの社内勉強会
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
ANDPADの社内勉強会
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
tenntenn さんに週1で社内勉強会を開催い
ただいている。



今の勉強会のテーマは

「静的解析とコード生成」

今日のおしながき
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
今日はこのあたり
https://docs.google.com/presentation/d/1I4pHnzV2dFOMbRcpA-XD0TaLcX6PBKpls6WxGHoMjOg/edit#sli
de=id.g80ffbfd5e3_0_168
コンパイラの世界の構文解析(式の表現)
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
構文解析
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
文字列を意味のある単位に分解して、関係性を明確化すること。

8 + 1
+
8 1
コンパイラの世界
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
https://www.sigbus.info/compilerbook
Ruiさんの

「低レイヤを知りたい人のためのCコンパイラ作成入門」

コンパイラの世界
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
① 木構造で文法構造を表現する (抽象構文木:AST)



② 構文の生成規則を作成する (BNFによる記述)



③ 字句解析と生成規則をアセンブリのコードに変換する

コンパイラの世界
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
6 + 2 * 3 + 3

字句解析
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
6 + 2 * 3 + 3

数値
 加算
 数値

抽象構文木
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
6 + 2 * 3 + 3

数値
 加算
 数値

四則演算
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
6 + 2 * 3 + 3

数値
 加算
 数値

アセンブラに
落とし込む
抽象構文木の構造をコードに落とし込むテクニック
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
expr = mul ("+" mul | "-" mul)*
mul = unary ("*" unary | "/" unary)*
unary = ("+" | "-")? primary
primary = num | "(" expr ")"
文法の生成規則をコンパクトなルール(EBNF)で記述する

https://www.sigbus.info/compilerbook
expr,mul,unary,primaryをコードに落とし込んでいく
静的解析の世界の構文解析(式の表現)
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
静的解析の世界
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
① go/parserパッケージでASTを取得する



② ASTを辿って演算を実装する

静的解析の世界
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
① go/parserパッケージでASTを取得する



② ASTを辿って演算を実装する

大変なところをほぼ
やってくれる



go/parser達
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
parser.ParseExprFrom

-> 式をパースする

parser.ParseExpr

-> 式をパースする(簡易版)

parser.ParseFile

-> ファイルからパースする

parser.ParseDir

-> ディレクトリからパースする(ParseFileを拡張)

go/parser
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
https://play.golang.org/p/UQjwTYeOWbB

6 + 2 * 3 + 3
parser.ParseExpr(`6 + 2 * 3 + 3`)
go/parser
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
https://play.golang.org/p/NzWJYarlBLB

(6 + 2) * 3 + 3

parser.ParseExpr(`(6 + 2) * 3 + 3`)
木構造で

計算の優先

順位とかも

考慮された

状態



四則演算で出てくる式を表すノードを覚える
ast.Expr インターフェース

*ast.BasicLit : 数値・文字列リテラル

*ast.ParenExpr : ()でくくられた式

*ast.UnaryExpr : 単項演算子 (-10とか)

*ast.BinaryExpr : 2項演算子 (1 + 2とか)

*ast.BasicLit
package ast // import "go/ast"
type BasicLit struct {
ValuePos token.Pos // literal position
Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', 'x7f', "foo" or `mno`
}
func (x *BasicLit) End() token.Pos
func (x *BasicLit) Pos() token.Pos
*ast.ParenExpr
package ast // import "go/ast"
type ParenExpr struct {
Lparen token.Pos // position of "("
X Expr // parenthesized expression
Rparen token.Pos // position of ")"
}
func (x *ParenExpr) End() token.Pos
func (x *ParenExpr) Pos() token.Pos
*ast.UnaryExpr
package ast // import "go/ast"
type UnaryExpr struct {
OpPos token.Pos // position of Op
Op token.Token // operator
X Expr // operand
}
func (x *UnaryExpr) End() token.Pos
func (x *UnaryExpr) Pos() token.Pos
*ast.BinaryExpr
package ast // import "go/ast"
type BinaryExpr struct {
X Expr // left operand
OpPos token.Pos // position of Op
Op token.Token // operator
Y Expr // right operand
}
func (x *BinaryExpr) End() token.Pos
func (x *BinaryExpr) Pos() token.Pos
四則演算を作っていく
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
go/parser
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
https://play.golang.org/p/NzWJYarlBLB

(6 + 2) * 3 + 3

式を表すノードが渡されたときのロジックを考える
*ast.BasicLit : 数値を返す

-> evaluateBasicLit()

*ast.ParenExpr : カッコの中身は式なので再評価

-> evaluateParenExpr()

*ast.UnaryExpr : +,-をつけて返す

-> evaluateUnaryExpr()

*ast.BinaryExpr : 右辺と左辺を計算(+,-,*,/,%)して返す

-> evaluateBinaryExpr()

evaluateExpr 大本の処理の振り分け
func evaluateExpr(expr ast.Expr) float64 {
switch e := expr.(type) {
case *ast.BasicLit:
return evaluateBasicLit(e)
case *ast.BinaryExpr:
return evaluateBinaryExpr(e)
case *ast.UnaryExpr:
return evaluateUnaryExpr(e)
case *ast.ParenExpr:
return evaluateParenExpr(e)
default:
panic("evaluateExpr error")
}
}
evaluateBasicLit
func evaluateBasicLit(basic *ast.BasicLit) float64{
switch basic.Kind {
case token.INT:
v, err := strconv.ParseInt(basic.Value,10,64)
if err != nil{
panic("int parse error")
}
return float64(v)
case token.FLOAT:
v, err := strconv.ParseFloat(basic.Value,64)
if err != nil{
panic("float parse error")
}
return v
default:
panic("BasicLit error")
}
}
evaluateParenExpr
func evaluateParenExpr(expr *ast.ParenExpr) float64{
x := evaluateExpr(expr.X)
return x
}
(1+2*3) みたいなカッコでくくられた式を評価する。

Xにさらに*ast.Exprが入っているので再起的に評価する。

evaluateUnaryExpr
func evaluateUnaryExpr(expr *ast.UnaryExpr) float64{
x := evaluateExpr(expr.X)
switch expr.Op {
case token.ADD:
return x
case token.SUB:
return -x
default:
panic("unaryExpr error")
}
}
evaluateBinaryExpr
func evaluateBinaryExpr(expr *ast.BinaryExpr) float64{
x := evaluateExpr(expr.X)
y := evaluateExpr(expr.Y)
switch expr.Op {
case token.ADD:
return x + y
case token.SUB:
return x - y
case token.MUL:
return x * y
case token.QUO:
return x / y
case token.REM:
return float64(int(x) % int(y))
default:
panic("binaryExpr error")
}
}
実行する
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
まとめ
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
まとめ
Confidential
Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
・四則演算を通じて構文解析を理解した
・https://github.com/daisuke0131/enzan

More Related Content

Similar to 歯磨き.go #2

How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?Hiroshi SHIBATA
 
How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?Hiroshi SHIBATA
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
"Logo Maker’s micro guts — micro frontend at Fiverr", Yuriy Dadichin
"Logo Maker’s micro guts — micro frontend at Fiverr", Yuriy Dadichin"Logo Maker’s micro guts — micro frontend at Fiverr", Yuriy Dadichin
"Logo Maker’s micro guts — micro frontend at Fiverr", Yuriy DadichinFwdays
 
Gartner: Top 10 Strategic Technology Trends 2016
Gartner: Top 10 Strategic Technology Trends 2016Gartner: Top 10 Strategic Technology Trends 2016
Gartner: Top 10 Strategic Technology Trends 2016Den Reymer
 
Gartner Top 10 Strategic Technology Trends 2016
Gartner Top 10 Strategic Technology Trends 2016 Gartner Top 10 Strategic Technology Trends 2016
Gartner Top 10 Strategic Technology Trends 2016 Deepak Kamboj
 
Identifying intellectual property component at the early stage
Identifying intellectual property component at the early stageIdentifying intellectual property component at the early stage
Identifying intellectual property component at the early stageRathish Chandra Gatti,Ph.D
 
Navigating the new route, Oil and Gas | Accenture
Navigating the new route, Oil and Gas | AccentureNavigating the new route, Oil and Gas | Accenture
Navigating the new route, Oil and Gas | Accentureaccenture
 
Managing the Cyberthreat Landscape
Managing the Cyberthreat LandscapeManaging the Cyberthreat Landscape
Managing the Cyberthreat LandscapeAT&T
 
Completing the Journey Stronger of Oil and Gas | Accenture
Completing the Journey Stronger of Oil and Gas | AccentureCompleting the Journey Stronger of Oil and Gas | Accenture
Completing the Journey Stronger of Oil and Gas | Accentureaccenture
 
Near Real-time Outlier Detection and Interpretation - Part 1 by Robert Thorma...
Near Real-time Outlier Detection and Interpretation - Part 1 by Robert Thorma...Near Real-time Outlier Detection and Interpretation - Part 1 by Robert Thorma...
Near Real-time Outlier Detection and Interpretation - Part 1 by Robert Thorma...DataWorks Summit/Hadoop Summit
 
Blackhat - Do you trust your Threat Intelligence
Blackhat - Do you trust your Threat IntelligenceBlackhat - Do you trust your Threat Intelligence
Blackhat - Do you trust your Threat IntelligenceChristopher Doman
 
Deep dive into Ruby's require - RubyConf Taiwan 2023
Deep dive into Ruby's require - RubyConf Taiwan 2023Deep dive into Ruby's require - RubyConf Taiwan 2023
Deep dive into Ruby's require - RubyConf Taiwan 2023Hiroshi SHIBATA
 
Growth Hacking for Lean Startups: How to Get, Keep and Grow Customers
Growth Hacking for Lean Startups:  How to Get, Keep and Grow CustomersGrowth Hacking for Lean Startups:  How to Get, Keep and Grow Customers
Growth Hacking for Lean Startups: How to Get, Keep and Grow CustomersChicago Lean Startup
 
Linux Foundation Live Webinar: Applying Governance to CI/CD
Linux Foundation Live Webinar: Applying Governance to CI/CDLinux Foundation Live Webinar: Applying Governance to CI/CD
Linux Foundation Live Webinar: Applying Governance to CI/CDTiffany Jachja
 
Getting Your Piece of the Mobile Pi 2017
Getting Your Piece of the Mobile Pi 2017Getting Your Piece of the Mobile Pi 2017
Getting Your Piece of the Mobile Pi 2017Doug Sillars
 
{unscripted} 2020 : A Conference for Simplifying and Scaling Software Delivery
{unscripted} 2020 : A Conference for Simplifying and Scaling Software Delivery{unscripted} 2020 : A Conference for Simplifying and Scaling Software Delivery
{unscripted} 2020 : A Conference for Simplifying and Scaling Software DeliveryTiffany Jachja
 

Similar to 歯磨き.go #2 (20)

How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?
 
How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
"Logo Maker’s micro guts — micro frontend at Fiverr", Yuriy Dadichin
"Logo Maker’s micro guts — micro frontend at Fiverr", Yuriy Dadichin"Logo Maker’s micro guts — micro frontend at Fiverr", Yuriy Dadichin
"Logo Maker’s micro guts — micro frontend at Fiverr", Yuriy Dadichin
 
Gartner: Top 10 Strategic Technology Trends 2016
Gartner: Top 10 Strategic Technology Trends 2016Gartner: Top 10 Strategic Technology Trends 2016
Gartner: Top 10 Strategic Technology Trends 2016
 
Gartner Top 10 Strategic Technology Trends 2016
Gartner Top 10 Strategic Technology Trends 2016 Gartner Top 10 Strategic Technology Trends 2016
Gartner Top 10 Strategic Technology Trends 2016
 
Identifying intellectual property component at the early stage
Identifying intellectual property component at the early stageIdentifying intellectual property component at the early stage
Identifying intellectual property component at the early stage
 
Navigating the new route, Oil and Gas | Accenture
Navigating the new route, Oil and Gas | AccentureNavigating the new route, Oil and Gas | Accenture
Navigating the new route, Oil and Gas | Accenture
 
Managing the Cyberthreat Landscape
Managing the Cyberthreat LandscapeManaging the Cyberthreat Landscape
Managing the Cyberthreat Landscape
 
Completing the Journey Stronger of Oil and Gas | Accenture
Completing the Journey Stronger of Oil and Gas | AccentureCompleting the Journey Stronger of Oil and Gas | Accenture
Completing the Journey Stronger of Oil and Gas | Accenture
 
A Snapshot of DevOps
A Snapshot of DevOpsA Snapshot of DevOps
A Snapshot of DevOps
 
Near Real-time Outlier Detection and Interpretation - Part 1 by Robert Thorma...
Near Real-time Outlier Detection and Interpretation - Part 1 by Robert Thorma...Near Real-time Outlier Detection and Interpretation - Part 1 by Robert Thorma...
Near Real-time Outlier Detection and Interpretation - Part 1 by Robert Thorma...
 
Near Real-Time Outlier Detection and Interpretation
Near Real-Time Outlier Detection and InterpretationNear Real-Time Outlier Detection and Interpretation
Near Real-Time Outlier Detection and Interpretation
 
Blackhat - Do you trust your Threat Intelligence
Blackhat - Do you trust your Threat IntelligenceBlackhat - Do you trust your Threat Intelligence
Blackhat - Do you trust your Threat Intelligence
 
Deep dive into Ruby's require - RubyConf Taiwan 2023
Deep dive into Ruby's require - RubyConf Taiwan 2023Deep dive into Ruby's require - RubyConf Taiwan 2023
Deep dive into Ruby's require - RubyConf Taiwan 2023
 
Growth Hacking for Lean Startups: How to Get, Keep and Grow Customers
Growth Hacking for Lean Startups:  How to Get, Keep and Grow CustomersGrowth Hacking for Lean Startups:  How to Get, Keep and Grow Customers
Growth Hacking for Lean Startups: How to Get, Keep and Grow Customers
 
IPSec VPN Basics
IPSec VPN BasicsIPSec VPN Basics
IPSec VPN Basics
 
Linux Foundation Live Webinar: Applying Governance to CI/CD
Linux Foundation Live Webinar: Applying Governance to CI/CDLinux Foundation Live Webinar: Applying Governance to CI/CD
Linux Foundation Live Webinar: Applying Governance to CI/CD
 
Getting Your Piece of the Mobile Pi 2017
Getting Your Piece of the Mobile Pi 2017Getting Your Piece of the Mobile Pi 2017
Getting Your Piece of the Mobile Pi 2017
 
{unscripted} 2020 : A Conference for Simplifying and Scaling Software Delivery
{unscripted} 2020 : A Conference for Simplifying and Scaling Software Delivery{unscripted} 2020 : A Conference for Simplifying and Scaling Software Delivery
{unscripted} 2020 : A Conference for Simplifying and Scaling Software Delivery
 

More from Daisuke Yamashita

Introduction of ios-chart in oss-labs#3
Introduction of ios-chart in oss-labs#3Introduction of ios-chart in oss-labs#3
Introduction of ios-chart in oss-labs#3Daisuke Yamashita
 
About SnapKit - Open source lab -
About SnapKit - Open source lab -About SnapKit - Open source lab -
About SnapKit - Open source lab -Daisuke Yamashita
 
Swift open source library - ViewMonitor -
Swift open source library - ViewMonitor -Swift open source library - ViewMonitor -
Swift open source library - ViewMonitor -Daisuke Yamashita
 
Let's Start Swift Open Source Activity.
Let's Start Swift Open Source Activity.Let's Start Swift Open Source Activity.
Let's Start Swift Open Source Activity.Daisuke Yamashita
 
バグのことは嫌いになってもXcodeのことは嫌いにならないでください。
バグのことは嫌いになってもXcodeのことは嫌いにならないでください。バグのことは嫌いになってもXcodeのことは嫌いにならないでください。
バグのことは嫌いになってもXcodeのことは嫌いにならないでください。Daisuke Yamashita
 
Unity Introduction from 2D shooting game.
Unity Introduction from 2D shooting game.Unity Introduction from 2D shooting game.
Unity Introduction from 2D shooting game.Daisuke Yamashita
 
Introduction of Swift from Machine Learning
Introduction of Swift from Machine LearningIntroduction of Swift from Machine Learning
Introduction of Swift from Machine LearningDaisuke Yamashita
 
Introduction of Swift from Game Development
Introduction of Swift from Game DevelopmentIntroduction of Swift from Game Development
Introduction of Swift from Game DevelopmentDaisuke Yamashita
 
How to measure UIView position on Native App
How to measure UIView position on Native AppHow to measure UIView position on Native App
How to measure UIView position on Native AppDaisuke Yamashita
 

More from Daisuke Yamashita (12)

Tensorflow
TensorflowTensorflow
Tensorflow
 
Introduction of ios-chart in oss-labs#3
Introduction of ios-chart in oss-labs#3Introduction of ios-chart in oss-labs#3
Introduction of ios-chart in oss-labs#3
 
About SnapKit - Open source lab -
About SnapKit - Open source lab -About SnapKit - Open source lab -
About SnapKit - Open source lab -
 
View Monitoring Tips
View Monitoring TipsView Monitoring Tips
View Monitoring Tips
 
Swift open source library - ViewMonitor -
Swift open source library - ViewMonitor -Swift open source library - ViewMonitor -
Swift open source library - ViewMonitor -
 
Let's Start Swift Open Source Activity.
Let's Start Swift Open Source Activity.Let's Start Swift Open Source Activity.
Let's Start Swift Open Source Activity.
 
バグのことは嫌いになってもXcodeのことは嫌いにならないでください。
バグのことは嫌いになってもXcodeのことは嫌いにならないでください。バグのことは嫌いになってもXcodeのことは嫌いにならないでください。
バグのことは嫌いになってもXcodeのことは嫌いにならないでください。
 
Unity Introduction from 2D shooting game.
Unity Introduction from 2D shooting game.Unity Introduction from 2D shooting game.
Unity Introduction from 2D shooting game.
 
OpenCV on mobile
OpenCV on mobileOpenCV on mobile
OpenCV on mobile
 
Introduction of Swift from Machine Learning
Introduction of Swift from Machine LearningIntroduction of Swift from Machine Learning
Introduction of Swift from Machine Learning
 
Introduction of Swift from Game Development
Introduction of Swift from Game DevelopmentIntroduction of Swift from Game Development
Introduction of Swift from Game Development
 
How to measure UIView position on Native App
How to measure UIView position on Native AppHow to measure UIView position on Native App
How to measure UIView position on Native App
 

Recently uploaded

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 

Recently uploaded (20)

9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

歯磨き.go #2

  • 1. Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 歯磨き.go #2 構文解析
  • 2. 歯磨き.goとは Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 一過性の勉強会ではなく、少しずつ無理のない範囲で 毎日の歯磨きのように続けていくことを目的とした ゆるふわなgo勉強会です。 (もともと社内でやってたけどスピンオフしました)
  • 3. 歯磨き.goとは Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 ・社内で静的解析の勉強会を始めたので  そのアウトプットの場になれば。会が進んできたら、 社内で 作った静的解析ツールを紹介(自慢)する場に  なれば良いなとか ・社内だけだと閉塞感でると思うので社外の方用のLT  枠も用意するのでぜひぜひ登壇したい方大歓迎。  ゆるふわな会なので怖く無いよ。
  • 4. 自己紹介 Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
  • 5. 自己紹介 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 山下 大輔 Daisuke Yamashita ものづくり系エンジニア
 go言語に限らずものづくりに関わる開発は 全般的に好き。
 
 毎日使う最強のツールを作るのが目標。

  • 6. 目次 Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
  • 7. 目次 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 ・ANDPADの社内勉強会 ・コンパイラの世界の構文解析 ・静的解析の世界の構文解析 
 ・静的解析を使って構文解析をして四則演算機を作る
  • 8. ANDPADの社内勉強会 Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
  • 9. ANDPADの社内勉強会 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 tenntenn さんに週1で社内勉強会を開催い ただいている。
 
 今の勉強会のテーマは
 「静的解析とコード生成」

  • 10. 今日のおしながき Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 今日はこのあたり https://docs.google.com/presentation/d/1I4pHnzV2dFOMbRcpA-XD0TaLcX6PBKpls6WxGHoMjOg/edit#sli de=id.g80ffbfd5e3_0_168
  • 11. コンパイラの世界の構文解析(式の表現) Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
  • 12. 構文解析 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 文字列を意味のある単位に分解して、関係性を明確化すること。
 8 + 1 + 8 1
  • 13. コンパイラの世界 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 https://www.sigbus.info/compilerbook Ruiさんの
 「低レイヤを知りたい人のためのCコンパイラ作成入門」

  • 14. コンパイラの世界 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 ① 木構造で文法構造を表現する (抽象構文木:AST)
 
 ② 構文の生成規則を作成する (BNFによる記述)
 
 ③ 字句解析と生成規則をアセンブリのコードに変換する

  • 15. コンパイラの世界 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 6 + 2 * 3 + 3

  • 16. 字句解析 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 6 + 2 * 3 + 3
 数値
 加算
 数値

  • 17. 抽象構文木 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 6 + 2 * 3 + 3
 数値
 加算
 数値

  • 18. 四則演算 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 6 + 2 * 3 + 3
 数値
 加算
 数値
 アセンブラに 落とし込む
  • 19. 抽象構文木の構造をコードに落とし込むテクニック Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 expr = mul ("+" mul | "-" mul)* mul = unary ("*" unary | "/" unary)* unary = ("+" | "-")? primary primary = num | "(" expr ")" 文法の生成規則をコンパクトなルール(EBNF)で記述する
 https://www.sigbus.info/compilerbook expr,mul,unary,primaryをコードに落とし込んでいく
  • 20. 静的解析の世界の構文解析(式の表現) Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
  • 21. 静的解析の世界 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 ① go/parserパッケージでASTを取得する
 
 ② ASTを辿って演算を実装する

  • 22. 静的解析の世界 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 ① go/parserパッケージでASTを取得する
 
 ② ASTを辿って演算を実装する
 大変なところをほぼ やってくれる
 

  • 23. go/parser達 Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 parser.ParseExprFrom
 -> 式をパースする
 parser.ParseExpr
 -> 式をパースする(簡易版)
 parser.ParseFile
 -> ファイルからパースする
 parser.ParseDir
 -> ディレクトリからパースする(ParseFileを拡張)

  • 24. go/parser Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 https://play.golang.org/p/UQjwTYeOWbB
 6 + 2 * 3 + 3 parser.ParseExpr(`6 + 2 * 3 + 3`)
  • 25. go/parser Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 https://play.golang.org/p/NzWJYarlBLB
 (6 + 2) * 3 + 3
 parser.ParseExpr(`(6 + 2) * 3 + 3`) 木構造で
 計算の優先
 順位とかも
 考慮された
 状態
 

  • 26. 四則演算で出てくる式を表すノードを覚える ast.Expr インターフェース
 *ast.BasicLit : 数値・文字列リテラル
 *ast.ParenExpr : ()でくくられた式
 *ast.UnaryExpr : 単項演算子 (-10とか)
 *ast.BinaryExpr : 2項演算子 (1 + 2とか)

  • 27. *ast.BasicLit package ast // import "go/ast" type BasicLit struct { ValuePos token.Pos // literal position Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', 'x7f', "foo" or `mno` } func (x *BasicLit) End() token.Pos func (x *BasicLit) Pos() token.Pos
  • 28. *ast.ParenExpr package ast // import "go/ast" type ParenExpr struct { Lparen token.Pos // position of "(" X Expr // parenthesized expression Rparen token.Pos // position of ")" } func (x *ParenExpr) End() token.Pos func (x *ParenExpr) Pos() token.Pos
  • 29. *ast.UnaryExpr package ast // import "go/ast" type UnaryExpr struct { OpPos token.Pos // position of Op Op token.Token // operator X Expr // operand } func (x *UnaryExpr) End() token.Pos func (x *UnaryExpr) Pos() token.Pos
  • 30. *ast.BinaryExpr package ast // import "go/ast" type BinaryExpr struct { X Expr // left operand OpPos token.Pos // position of Op Op token.Token // operator Y Expr // right operand } func (x *BinaryExpr) End() token.Pos func (x *BinaryExpr) Pos() token.Pos
  • 31. 四則演算を作っていく Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
  • 32. go/parser Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 https://play.golang.org/p/NzWJYarlBLB
 (6 + 2) * 3 + 3

  • 33. 式を表すノードが渡されたときのロジックを考える *ast.BasicLit : 数値を返す
 -> evaluateBasicLit()
 *ast.ParenExpr : カッコの中身は式なので再評価
 -> evaluateParenExpr()
 *ast.UnaryExpr : +,-をつけて返す
 -> evaluateUnaryExpr()
 *ast.BinaryExpr : 右辺と左辺を計算(+,-,*,/,%)して返す
 -> evaluateBinaryExpr()

  • 34. evaluateExpr 大本の処理の振り分け func evaluateExpr(expr ast.Expr) float64 { switch e := expr.(type) { case *ast.BasicLit: return evaluateBasicLit(e) case *ast.BinaryExpr: return evaluateBinaryExpr(e) case *ast.UnaryExpr: return evaluateUnaryExpr(e) case *ast.ParenExpr: return evaluateParenExpr(e) default: panic("evaluateExpr error") } }
  • 35. evaluateBasicLit func evaluateBasicLit(basic *ast.BasicLit) float64{ switch basic.Kind { case token.INT: v, err := strconv.ParseInt(basic.Value,10,64) if err != nil{ panic("int parse error") } return float64(v) case token.FLOAT: v, err := strconv.ParseFloat(basic.Value,64) if err != nil{ panic("float parse error") } return v default: panic("BasicLit error") } }
  • 36. evaluateParenExpr func evaluateParenExpr(expr *ast.ParenExpr) float64{ x := evaluateExpr(expr.X) return x } (1+2*3) みたいなカッコでくくられた式を評価する。
 Xにさらに*ast.Exprが入っているので再起的に評価する。

  • 37. evaluateUnaryExpr func evaluateUnaryExpr(expr *ast.UnaryExpr) float64{ x := evaluateExpr(expr.X) switch expr.Op { case token.ADD: return x case token.SUB: return -x default: panic("unaryExpr error") } }
  • 38. evaluateBinaryExpr func evaluateBinaryExpr(expr *ast.BinaryExpr) float64{ x := evaluateExpr(expr.X) y := evaluateExpr(expr.Y) switch expr.Op { case token.ADD: return x + y case token.SUB: return x - y case token.MUL: return x * y case token.QUO: return x / y case token.REM: return float64(int(x) % int(y)) default: panic("binaryExpr error") } }
  • 39. 実行する Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止
  • 40. まとめ Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止
  • 41. まとめ Confidential Copyright © 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載・無断複製の禁止 ・四則演算を通じて構文解析を理解した ・https://github.com/daisuke0131/enzan