Skip to content

Instantly share code, notes, and snippets.

@junjis0203
Created May 27, 2018 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junjis0203/d97c4fc5384ccaff231b60c2c9e6d2b4 to your computer and use it in GitHub Desktop.
Save junjis0203/d97c4fc5384ccaff231b60c2c9e6d2b4 to your computer and use it in GitHub Desktop.
File struct(hand parsing) for Go program calculating pi using monte carlo algorithm
/*
package main
import (
"fmt"
"math/rand"
)
func worker(n int, ch chan int) {
count := 0
for i := 0; i < n; i++ {
x := rand.Float64()
y := rand.Float64()
if x*x+y*y <= 1 {
count += 1
}
}
ch <- count
}
func montecarlo(n, proc int) float64 {
work := n / proc
ch := make(chan int, proc)
for i := 0; i < proc; i++ {
go worker(work, ch)
}
sum := 0
for i := 0; i < proc; i++ {
sum += <-ch
}
return (float64(sum) / float64(n)) * 4
}
func main() {
pi := montecarlo(10000*10000, 100)
fmt.Printf("pi = %v\n", pi)
}
*/
File{
PkgName: Name{Value: "main"},
DeclList: {
ImportDecl{Path: BasicLit{Value: "fmt", Kind: StringLit}},
ImportDecl{Path: BasicLit{Value: "math/rand", Kind: StringLit}},
FuncDecl{
Name{Value: "worker"},
Type: FuncType{
ParamList: {
Field{Name: Name{Value: "n"}, Type: Name{Value: "int"}},
Field{Name: Name{Value: "ch"}, Type: ChanType{Elem: Name{Value: "int"}}}
},
ResultList: {}
},
Body: BlockStmt{
List: {
AssignStmt{Op: Def, Lhs: Name{Value: "count"}, Rhs: BasicLit{Value: "0", Kind: IntLit}},
ForStmt{
Init: AssignStmt{Op: Def, Lhs: Name{Value: "i"}, Rhs: BasicLit{Value: "0", Kind: IntLit}},
Cond: Operation{Op: Lss, X: Name{Value: "i"}, Y: Name{Value: "n"}},
Post: AssignStmt{Op: Add, Lhs: Name{Value: "i"}, Rhs: BasicLit{Value: "1", Kind: IntLit}},
Body: BlockStmt{
List: {
AssignStmt{
Op: Def,
Lhs: Name{Value: "x"},
Rhs: CallExpr{Fun: SelectorExpr{X: Name{Value: "rand"}, Sel: "Float64"}, ArgList: {}}
},
AssignStmt{
Op: Def,
Lhs: Name{Value: "y"},
Rhs: CallExpr{Fun: SelectorExpr{X: Name{Value: "rand"}, Sel: "Float64"}, ArgList: {}}
},
IfStmt{
Cond:
Operation{
Op: Leq,
Lhs: Operation{
Op: Add,
Lhs: Operation{Op: Mul, Lhs: Name{Value: "x"}, Rhs: Name{Value: "x"}},
Rhs: Operation{Op: Mul, Lhs: Name{Value: "y"}, Rhs: Name{Value: "y"}}
},
Rhs: BasicLit{Value: "1", Kind: IntLit}
}
},
Then: BlockStmt{
List: {
AssignStmt{Op: Add, Lhs: Name{Value: "count"}, Rhs: BasicLit{Value: "1", Kind: IntLit}}
}
}
}
}
}
},
SendStmt{Chan: Name{Value: "ch"}, Value: Name{Value: "count"}}
}
}
},
FuncDecl{
Name{Value: "montecarlo"},
Type: FuncType{
ParamList: {
Field{Name: Name{Value: "n"}, Type: Name{Value: "int"}},
Field{Name: Name{Value: "proc"}, Type: Name{Value: "int"}}
},
ResultList: {
Field{Type: Name:{Value: "float64"}}
}
},
Body: BlockStmt{
List: {
AssignStmt{
Op: Def,
Lhs: Name{Value: "work"},
Rhs: Operation{Op: Div, Lhs: Name{Value: "n"}, Rhs: Name{Value: "proc"}}
},
AssignStmt{
Op: Def,
Lhs: Name{Value: "ch"},
Rhs: CallExpr{
Fun: Name{Value: "make"},
ArgList: {ChanType{Elem: Name{Value: "int"}}, Name{Vaue: "proc"}}
}
},
ForStmt{
Init: AssignStmt{Op: Def, Lhs: Name{Value: "i"}, Rhs: BasicLit{Value: "0", Kind: IntLit}},
Cond: Operation{Op: Lss, X: Name{Value: "i"}, Y: Name{Value: "proc"}},
Post: AssignStmt{Op: Add, Lhs: Name{Value: "i"}, Rhs: BasicLit{Value: "1", Kind: IntLit}},
Body: BlockStmt{
List: {
CallStmt{
Tok: _Go,
CallExpr{Fun: Name{Value: "worker"}, ArgList: {Name{Value: "work"}, Name{Vaue: "ch"}}
}
}
}
},
AssignStmt{Op: Def, Lhs: Name{Value: "sum"}, Rhs: BasicLit{Value: "0", Kind: IntLit}},
ForStmt{
Init: AssignStmt{Op: Def, Lhs: Name{Value: "i"}, Rhs: BasicLit{Value: "0", Kind: IntLit}},
Cond: Operation{Op: Lss, X: Name{Value: "i"}, Y: Name{Value: "proc"}},
Post: AssignStmt{Op: Add, Lhs: Name{Value: "i"}, Rhs: BasicLit{Value: "1", Kind: IntLit}},
Body: BlockStmt{
List: {
AssignStmt{Op: Add, Lhs: Name{Value: "sum"}, Rhs: Operation{Op: Recv, X: Name{Value: "ch"}}}
}
}
},
ReturnStmt{
Result: Operation{
Op: Mul,
Lhs: Operation{
Op: Div,
Lhs: CallExpr{Fun: Name{Value: "float64"}, ArgList: {Name{Value: "sum"}},
Rhs: CallExpr{Fun: Name{Value: "float64"}, ArgList: {Name{Value: "n"}}
}
Rhs: BasicLit{Value: "4", Kind: IntLit}
}
}
}
}
},
FuncDecl{
Name{Value: "main"},
Type: FuncType{
ParamList: {},
ResultList: {}
},
Body: BlockStmt{
List: {
AssignStmt{
Op: Def,
Lhs: Name{Value: "pi"},
Rhs: CallExpr{
Fun: Name{Value: "montecarlo"},
ArgList: {
Operation{
Op: Mul,
Lhs: BasicLit{Value: "10000", Kind: IntLit}, Rhs: BasicLit{Value: "10000", Kind: IntLit}
},
BasicLit{Value: "100", Kind: IntLit}
}
}
},
ExprStmt{
X: CallExpr{
Fun: SelectorExpr{X: Name{Value: "fmt"}, Sel: "Printf"},
ArgList: {
BasicLit{Value: "pi = %v\n", Kind: StringLit},
Name{Value: "pi"}
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment