-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
205 lines (185 loc) · 4.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/spf13/cobra"
"github.com/pzbitskiy/tealang/compiler"
dr "github.com/pzbitskiy/tealang/dryrun"
)
var outFile string
var inFile string
var source string
var compileOnly bool
var verbose bool
var oneliner string
var stdout bool
var raw bool
var dryrun string
var currentDir string
var sourceDir string
var sourceFile string
const defaultInFile string = "a.tl"
var rootCmd = &cobra.Command{
Use: "tealang [flags] source-file",
Short: "Tealang compiler for Algorand Smart Contract (ASC1)",
Long: `Tealang compiler (ASC1)
Documentation: https://github.com/pzbitskiy/tealang
Syntax highlighter for vscode: https://github.com/pzbitskiy/tealang-syntax-highlighter`,
DisableFlagsInUseLine: true,
Args: func(cmd *cobra.Command, args []string) (err error) {
if len(oneliner) > 0 {
source = oneliner
inFile = defaultInFile
return nil
}
if len(args) < 1 {
return errors.New("requires a source file name or - for stdin")
}
inFile = args[0]
if inFile == "-" {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
source = string(data)
inFile = defaultInFile
return nil
}
currentDir, err := os.Getwd()
if err != nil {
return err
}
fullPath := path.Join(currentDir, inFile)
srcBytes, err := ioutil.ReadFile(fullPath)
if err != nil {
return err
}
sourceDir = path.Dir(fullPath)
sourceFile = path.Base(fullPath)
source = string(srcBytes)
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if cmd.Flags().Changed("stdout") && cmd.Flags().Changed("output") {
fmt.Printf("only one of [--stdout] or [--output] can be specified")
os.Exit(1)
}
if cmd.Flags().Changed("raw") && !cmd.Flags().Changed("raw") {
fmt.Printf("[--raw] might be only used with [--stdout]")
os.Exit(1)
}
var prog compiler.TreeNodeIf
var parseErrors []compiler.ParserError
var teal string
var bytecode []byte
var err error
var op *logic.OpStream
if len(oneliner) > 0 {
prog, parseErrors = compiler.ParseOneLineCond(source)
if len(parseErrors) > 0 {
for _, e := range parseErrors {
fmt.Printf("%s\n", e.String())
}
os.Exit(1)
}
} else {
input := compiler.InputDesc{
Source: source,
SourceFile: sourceFile,
SourceDir: sourceDir,
CurrentDir: currentDir,
}
prog, parseErrors = compiler.ParseProgram(input)
if len(parseErrors) > 0 {
for _, e := range parseErrors {
fmt.Printf("%s\n", e.String())
}
os.Exit(1)
}
}
teal = compiler.Codegen(prog)
if !compileOnly {
op, err = logic.AssembleString(teal)
if err != nil {
for _, err := range op.Errors {
fmt.Println(err)
}
fmt.Println(err.Error())
os.Exit(1)
}
bytecode = op.Program
}
if stdout {
output := teal
if bytecode != nil {
if raw {
output = string(bytecode)
} else {
output = hex.Dump(bytecode)
}
}
fmt.Print(output)
} else {
ext := path.Ext(inFile)
if outFile == "" {
if compileOnly {
outFile = inFile[0:len(inFile)-len(ext)] + ".teal"
} else {
outFile = inFile[0:len(inFile)-len(ext)] + ".tok"
}
}
if verbose {
fmt.Printf("Writing result to %s\n", outFile)
}
output := []byte(teal)
if bytecode != nil {
output = bytecode
}
ioutil.WriteFile(outFile, output, 0644)
}
if cmd.Flags().Changed("dryrun") {
if bytecode == nil {
op, err = logic.AssembleString(teal)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
bytecode = op.Program
}
sb := strings.Builder{}
pass, err := dr.Run(bytecode, dryrun, &sb)
fmt.Printf("trace:\n%s\n", sb.String())
if pass {
fmt.Printf(" - pass -\n")
} else {
fmt.Printf("REJECT\n")
}
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
}
},
}
func setRootCmdFlags() {
rootCmd.Flags().StringVarP(&outFile, "output", "o", "", "write output to this file")
rootCmd.Flags().BoolVarP(&compileOnly, "compile", "c", false, "compile to TEAL assembler, do not produce bytecode")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
rootCmd.Flags().StringVarP(&oneliner, "oneliner", "l", "", "compile logic one-liner like '(txn.Sender == \"abc\") && (1+2) >= 3'")
rootCmd.Flags().BoolVarP(&stdout, "stdout", "s", false, "write output to stdout instead of a file")
rootCmd.Flags().BoolVarP(&raw, "raw", "r", false, "do not hex-encode bytecode when outputting to stdout")
rootCmd.Flags().StringVarP(&dryrun, "dryrun", "d", "", "dry run program with transaction data from the file provided")
}
func main() {
setRootCmdFlags()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}