restructure project
This commit is contained in:
137
cmd/monkey/main.go
Normal file
137
cmd/monkey/main.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/profile"
|
||||
|
||||
"monkey/internal"
|
||||
"monkey/internal/compiler"
|
||||
"monkey/internal/lexer"
|
||||
"monkey/internal/object"
|
||||
"monkey/internal/parser"
|
||||
"monkey/internal/repl"
|
||||
)
|
||||
|
||||
var (
|
||||
engine string
|
||||
interactive bool
|
||||
compile bool
|
||||
version bool
|
||||
debug bool
|
||||
|
||||
profileCPU bool
|
||||
profileMem bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] [<filename>]\n", path.Base(os.Args[0]))
|
||||
flag.PrintDefaults()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
flag.BoolVar(&version, "v", false, "display version information")
|
||||
flag.BoolVar(&debug, "d", false, "enable debug mode")
|
||||
flag.BoolVar(&compile, "c", false, "compile input to bytecode")
|
||||
|
||||
flag.BoolVar(&interactive, "i", false, "enable interactive mode")
|
||||
flag.StringVar(&engine, "e", "vm", "engine to use (eval or vm)")
|
||||
|
||||
flag.BoolVar(&profileCPU, "profile-cpu", false, "Enable CPU profiling.")
|
||||
flag.BoolVar(&profileMem, "profile-mem", false, "Enable Memory profiling.")
|
||||
}
|
||||
|
||||
// Indent indents a block of text with an indent string
|
||||
func Indent(text, indent string) string {
|
||||
if text[len(text)-1:] == "\n" {
|
||||
result := ""
|
||||
for _, j := range strings.Split(text[:len(text)-1], "\n") {
|
||||
result += indent + j + "\n"
|
||||
}
|
||||
return result
|
||||
}
|
||||
result := ""
|
||||
for _, j := range strings.Split(strings.TrimRight(text, "\n"), "\n") {
|
||||
result += indent + j + "\n"
|
||||
}
|
||||
return result[:len(result)-1]
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if version {
|
||||
fmt.Printf("%s %s", path.Base(os.Args[0]), internal.FullVersion())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
user, err := user.Current()
|
||||
if err != nil {
|
||||
log.Fatalf("could not determine current user: %s", err)
|
||||
}
|
||||
|
||||
args := flag.Args()
|
||||
|
||||
if profileCPU {
|
||||
defer profile.Start(profile.CPUProfile).Stop()
|
||||
} else if profileMem {
|
||||
defer profile.Start(profile.MemProfile).Stop()
|
||||
}
|
||||
|
||||
if compile {
|
||||
if len(args) < 1 {
|
||||
log.Fatal("no source file given to compile")
|
||||
}
|
||||
f, err := os.Open(args[0])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
b, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
l := lexer.New(string(b))
|
||||
p := parser.New(l)
|
||||
|
||||
program := p.ParseProgram()
|
||||
if len(p.Errors()) != 0 {
|
||||
log.Fatal(p.Errors())
|
||||
}
|
||||
|
||||
c := compiler.New()
|
||||
err = c.Compile(program)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
code := c.Bytecode()
|
||||
fmt.Printf("Main:\n%s\n", code.Instructions)
|
||||
|
||||
fmt.Print("Constants:\n")
|
||||
for i, constant := range code.Constants {
|
||||
fmt.Printf("%04d %s\n", i, constant.Inspect())
|
||||
if fn, ok := constant.(*object.CompiledFunction); ok {
|
||||
fmt.Printf("%s\n", Indent(fn.Instructions.String(), " "))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
opts := &repl.Options{
|
||||
Debug: debug,
|
||||
Engine: engine,
|
||||
Interactive: interactive,
|
||||
}
|
||||
repl := repl.New(user.Username, args, opts)
|
||||
repl.Run()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user