-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (71 loc) · 1.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
package main
import (
"fmt"
"net/http"
"os"
"os/user"
"html/template"
"github.com/gorilla/mux"
"github.com/roneetkumar/simple/evaluator"
"github.com/roneetkumar/simple/lexer"
"github.com/roneetkumar/simple/object"
"github.com/roneetkumar/simple/parser"
)
var templates *template.Template
func main() {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is the Simple programming language!\n",
user.Username)
fmt.Printf("Simply GO... \n")
//PARSING HTML
templates = template.Must(template.ParseGlob("web/templates/*.html"))
//PARSING CSS
fs := http.FileServer(http.Dir("web/templates/assets/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
//CREATING ROUTER INSTANCE
mux := mux.NewRouter()
//DEFINING ROUTES
mux.HandleFunc("/", indexHandler).Methods("GET")
mux.HandleFunc("/about", aboutHandler).Methods("GET")
mux.HandleFunc("/doc", docHandler).Methods("GET")
http.Handle("/", mux)
//DEFINING PORTS
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
http.ListenAndServe(":"+port, nil)
}
// Start func
func Start(str string) string {
env := object.NewEnvironment()
l := lexer.New(str)
p := parser.New(l)
program := p.ParseProgram()
evaluated := evaluator.Eval(program, env)
if evaluated != nil {
return evaluated.Inspect()
}
return ""
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code")
output := Start(code)
data := struct {
Code, Output string
}{
Code: code,
Output: output,
}
fmt.Println(data.Output)
templates.ExecuteTemplate(w, "index.html", data)
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<h1>About</h1>")
}
func docHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "doc.html", nil)
}