-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (69 loc) · 1.59 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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/aniketp/meego/src/ast"
"github.com/aniketp/meego/src/checker"
"github.com/aniketp/meego/src/codegen"
"github.com/aniketp/meego/src/lexer"
"github.com/aniketp/meego/src/parser"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func Parse(input string) *ast.Program {
l := lexer.NewLexer([]byte(input))
p := parser.NewParser()
node, err := p.Parse(l)
check(err)
program, _ := node.(*ast.Program)
return program
}
func TypeCheck(program *ast.Program) {
err := checker.Checker(program)
check(err)
}
func Compile(code bytes.Buffer) string {
f, err := os.Create("./input/main.cpp")
check(err)
defer f.Close()
f.Write(code.Bytes())
var out bytes.Buffer
cmd1 := exec.Command("g++", "-std=c++11", "-o", "apple", "./input/main.cpp",
"./input/Builtins.cpp")
cmd1.Stderr = &out
err = cmd1.Run()
// Check if output was a valid one
if len(out.String()) != 0 {
panic(fmt.Sprintf("error: %s", out.String()))
}
// Now, execute the resulting 'apple' binary
cmd := exec.Command("./apple")
var outb bytes.Buffer
cmd.Stdout = &outb
err = cmd.Run()
check(err)
// This is the generated output of our transpiled program
return outb.String()
}
func main() {
if len(os.Args) < 2 {
panic("Provide a valid file name")
}
path := os.Args[1]
absPath, _ := filepath.Abs(path)
input, err := ioutil.ReadFile(absPath)
check(err)
program := Parse(string(input))
TypeCheck(program)
// Generate vanilla C++
code := codegen.GenWrapper(program)
result := Compile(code)
fmt.Println(result)
}