From 790a12706ca7727369818f73072bd2684d9064fa Mon Sep 17 00:00:00 2001 From: zerofruit <14wnrkim@gmail.com> Date: Sat, 23 Feb 2019 00:43:09 +0900 Subject: [PATCH] feat: fix cli print format --- cmd/compile/compile.go | 17 ++++++++++++----- cmd/koa.go | 4 +++- cmd/lex/lex.go | 4 ++-- cmd/repl/repl.go | 38 ++++++++++++++++++++++++++++++++++++-- test/test_execute.koa | 15 +++++++++++++++ translate/compiler.go | 2 +- 6 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 test/test_execute.koa diff --git a/cmd/compile/compile.go b/cmd/compile/compile.go index aa51a68b..88528321 100644 --- a/cmd/compile/compile.go +++ b/cmd/compile/compile.go @@ -12,7 +12,7 @@ import ( "github.com/urfave/cli" ) -type CompileResult struct { +type Result struct { Abi *abi.ABI Asm string RawByte string @@ -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()), } @@ -67,6 +75,5 @@ func compile(path string) error { } fmt.Println(string(b)) - return nil } diff --git a/cmd/koa.go b/cmd/koa.go index c20cf369..90b4d1bd 100644 --- a/cmd/koa.go +++ b/cmd/koa.go @@ -42,7 +42,7 @@ const koa = ` #### # # # # # # # # ###### # # # # # # - # # #### # # @DE-labtory/koa v0.0.1 + # # #### # # @DE-labtory/koa v0.1.0 ` @@ -50,7 +50,9 @@ const koa = ` 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") } diff --git a/cmd/lex/lex.go b/cmd/lex/lex.go index 5a8e742e..1a2b7dc1 100644 --- a/cmd/lex/lex.go +++ b/cmd/lex/lex.go @@ -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() diff --git a/cmd/repl/repl.go b/cmd/repl/repl.go index cf8285a6..6a933923 100644 --- a/cmd/repl/repl.go +++ b/cmd/repl/repl.go @@ -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" @@ -39,7 +43,7 @@ const koa = ` #### # # # # # # # # ###### # # # # # # - # # #### # # @DE-labtory/koa v0.0.1 + # # #### # # @DE-labtory/koa v0.1.0 ` @@ -47,6 +51,7 @@ const koa = ` 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") } @@ -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() @@ -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() } } diff --git a/test/test_execute.koa b/test/test_execute.koa new file mode 100644 index 00000000..00d3cb3e --- /dev/null +++ b/test/test_execute.koa @@ -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 + } +} \ No newline at end of file diff --git a/translate/compiler.go b/translate/compiler.go index a309b906..65c30d93 100644 --- a/translate/compiler.go +++ b/translate/compiler.go @@ -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 {