Skip to content

Commit

Permalink
upload 10-16
Browse files Browse the repository at this point in the history
  • Loading branch information
haifenghuang authored Jul 1, 2022
1 parent 0e48402 commit 479f49e
Show file tree
Hide file tree
Showing 82 changed files with 14,442 additions and 0 deletions.
105 changes: 105 additions & 0 deletions 10/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"fmt"
"magpie/eval"
"magpie/lexer"
"magpie/parser"
"magpie/token"
"os"
)

func TestLexer() {
input := " let x = 2 + (3 * 4) / ( 5 - 3 ) + 10 - a ** 2;"
fmt.Printf("Input = %s\n", input)

l := lexer.NewLexer(input)
for {
tok := l.NextToken()
fmt.Printf("Type: %s, Literal = %s\n", tok.Type, tok.Literal)
if tok.Type == token.TOKEN_EOF {
break
}
}
}

func TestParser() {
input := " 2 + (3 * 4) / ( 5 - 3 ) + 10 + 2 ** 2 ** 3 + xyz"
expected := "((((2 + ((3 * 4) / (5 - 3))) + 10) + (2 ** (2 ** 3))) + xyz)"
l := lexer.NewLexer(input)
p := parser.NewParser(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
for _, err := range p.Errors() {
fmt.Println(err)
}
os.Exit(1)
}
if program.String() != expected {
fmt.Printf("Syntax error: expected %s, got %s\n", expected, program.String())
os.Exit(1)
}

fmt.Printf("input = %s\n", input)
fmt.Printf("output = %s\n", program.String())
}

func TestEval() {
tests := []struct {
input string
expected string
}{
{"-1 - 2.333", "-3.333"},
{"1 + 2", "3"},
{"2 + (3 * 4) / ( 6 - 3 ) + 10", "16"},
{"2 + 3 * 4 / 6 - 3 + 10", "11"},
{"(5 + 2) * (4 - 2) + 6", "20"},
{"5 + 2 * 4 - 2 + 6", "17"},
{"5 + 2.1 * 4 - 2 + 6.2", "17.6"},
{"2 + 2 ** 2 ** 3", "258"},
{"10", "10"},
{"nil", "nil"},
{"true", "true"},
{"false", "false"},
{"let x = 2 + (3 * 4) / ( 6 - 3 ) + 10; x", "16"},
{"let x = 2 + (3 * 4) / ( 6 - 3 ) + 10; y", "error"},
{`{ let x = 10 { x } }`, "10"},
{"let x = \"hello world\"; x", "hello world"},
{`let x = "hello " + "world"; x`, "hello world"},
}

for _, tt := range tests {
l := lexer.NewLexer(tt.input)
p := parser.NewParser(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
for _, err := range p.Errors() {
fmt.Println(err)
}
break
}

scope := eval.NewScope(nil, os.Stdout)
evaluated := eval.Eval(program, scope)
if evaluated != nil {
if evaluated.Inspect() != tt.expected {
fmt.Printf("%s\n", evaluated.Inspect())
} else {
fmt.Printf("%s = %s\n", tt.input, tt.expected)
}
}
}
}

func main() {
args := os.Args[1:]
if len(args) == 1 {
if args[0] == "--lexer" {
TestLexer()
} else if args[0] == "--parser" {
TestParser()
}
os.Exit(0)
}
TestEval()
}
1 change: 1 addition & 0 deletions 10/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add 'String' handling
37 changes: 37 additions & 0 deletions 10/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
export GOPATH=$(pwd)

# for newer go version's build error: "package XXX is not in GOROOT"
export GO111MODULE=off

# format each go file
echo "Formatting go file..."
go fmt ./main.go > /dev/null
echo " main.go"

for file in `find ./src/magpie -name "*.go"`; do
echo " `basename $file`"
go fmt $file > /dev/null
done

interpreter_name=magpie

# cross-compiling
platforms=("windows/amd64" "linux/amd64" "darwin/amd64")
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=$interpreter_name'-'$GOOS'-'$GOARCH
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi

echo "Building ${interpreter_name} ( $GOOS )... ($output_name)"
env GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-s -w" -o $output_name main.go
if [ $? -ne 0 ]; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
fi
done
Loading

0 comments on commit 479f49e

Please sign in to comment.