Skip to content

Commit

Permalink
feat: fix cli print format
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroFruit committed Feb 22, 2019
1 parent df9abd9 commit 790a127
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
17 changes: 12 additions & 5 deletions cmd/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/urfave/cli"
)

type CompileResult struct {
type Result struct {
Abi *abi.ABI
Asm string
RawByte string
Expand Down Expand Up @@ -50,13 +50,21 @@ func compile(path string) error {
return err
}

abi, err := translate.ExtractAbi(*contract)
ab, err := translate.ExtractAbi(*contract)
if err != nil {
return err
}

result := CompileResult{
Abi: abi,
if err := PrintCompileResult(asm, ab); err != nil {
return err
}

return nil
}

func PrintCompileResult(asm translate.Asm, ab *abi.ABI) error {
result := Result{
Abi: ab,
Asm: asm.String(),
RawByte: fmt.Sprintf("%x", asm.ToRawByteCode()),
}
Expand All @@ -67,6 +75,5 @@ func compile(path string) error {
}

fmt.Println(string(b))

return nil
}
4 changes: 3 additions & 1 deletion cmd/koa.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ const koa = `
#### # # # #
# # # # ######
# # # # # #
# # #### # # @DE-labtory/koa v0.0.1
# # #### # # @DE-labtory/koa v0.1.0
`

func PrintLogo() {
color.Yellow(koa)
bold := color.New(color.Bold)
hiBlack := color.New(color.BgHiBlack)
fmt.Printf("The project is inspired by the simplicity and the ivy-bitcoin. The koa project is to create \na high-level language that has more expressions than the bitcoin script and is simpler and easy to analyze than soldity(ethereum).\n\n")
hiBlack.Printf("https://github.com/DE-labtory/koa \n")
bold.Print("Use exit() or Ctrl-c to exit \n")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/lex/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func lex(path string) error {
return err
}
l := parse.NewLexer(string(file))
printTokens(l)
PrintTokens(l)
return nil
}

func printTokens(l *parse.Lexer) {
func PrintTokens(l *parse.Lexer) {
for token := l.NextToken(); token.Type != parse.Eof; {
fmt.Println(token)
token = l.NextToken()
Expand Down
38 changes: 36 additions & 2 deletions cmd/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ import (
"fmt"
"io"

"github.com/DE-labtory/koa/translate"

"bufio"
"os"

compile_cmd "github.com/DE-labtory/koa/cmd/compile"
lex_cmd "github.com/DE-labtory/koa/cmd/lex"
parse_cmd "github.com/DE-labtory/koa/cmd/parse"
"github.com/DE-labtory/koa/parse"
"github.com/fatih/color"
Expand All @@ -39,14 +43,15 @@ const koa = `
#### # # # #
# # # # ######
# # # # # #
# # #### # # @DE-labtory/koa v0.0.1
# # #### # # @DE-labtory/koa v0.1.0
`

func PrintLogo() {
color.Yellow(koa)
bold := color.New(color.Bold)
bold.Printf("github: https://github.com/DE-labtory/koa \n\n")
fmt.Printf("The project is inspired by the simplicity and the ivy-bitcoin. The koa project is to create \na high-level language that has more expressions than the bitcoin script and is simpler and easy to analyze than soldity(ethereum).\n\n")
bold.Print("Use exit() or Ctrl-c to exit \n")
}
Expand All @@ -59,6 +64,8 @@ func Run() {
func run(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)

bold := color.New(color.Bold)

for {
fmt.Printf(PROMPT)
scanned := scanner.Scan()
Expand All @@ -73,13 +80,40 @@ func run(in io.Reader, out io.Writer) {
}

l := parse.NewLexer(line)
l2 := parse.NewLexer(line)

buf := parse.NewTokenBuffer(l)
contract, err := parse.Parse(buf)

asm, err := translate.CompileContract(*contract)
if err != nil {
color.Red(err.Error())
continue
}

ab, err := translate.ExtractAbi(*contract)
if err != nil {
color.Red(err.Error())
}

if err != nil {
fmt.Println(err.Error())
color.Red(err.Error())
continue
}

bold.Println("-->> LEX RESULT <<-----------------------------------------------")
lex_cmd.PrintTokens(l2)
fmt.Println()

bold.Println("-->> PARSE RESULT <<-----------------------------------------------")
fmt.Println(parse_cmd.PrintContract(contract))
fmt.Println()

bold.Println("-->> COMPILE RESULT <<-----------------------------------------------")
if err := compile_cmd.PrintCompileResult(asm, ab); err != nil {
color.Red(err.Error())
continue
}
fmt.Println()
}
}
15 changes: 15 additions & 0 deletions test/test_execute.koa
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
contract {
func addVariable() int {
int a = 5
int b = 10
return a + b
}

func addNative() int {
return 5 + 10
}

func addArgs(a int, b int) int {
return a + b
}
}
2 changes: 1 addition & 1 deletion translate/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func CompileContract(c ast.Contract) (Asm, error) {
return *asm, err
}
}

// Compile Memory size with updated memory table.
// And replace expected memory size with new memory size of the memory table.
if err := compileMemSize(asm, memTracer); err != nil {
Expand Down

0 comments on commit 790a127

Please sign in to comment.