Skip to content

Commit

Permalink
add exit function in order to exit from current project
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed Jul 4, 2022
1 parent 49f5bfe commit 9a8d84b
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 44 deletions.
Binary file added docs/ninja-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion evaluator/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,5 @@ func TestArrayMethodWrongUsage(t *testing.T) {
t.Errorf("erro expected \"%s\". Got: %s", tt.expectedErrorMessage, errObj.Message)
}
})

}
}
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ module ninja
go 1.17

require (
github.com/chzyer/readline v1.5.0
github.com/TheZoraiz/ascii-image-converter v1.12.0
github.com/fatih/color v1.13.0
github.com/spf13/pflag v1.0.5
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/fogleman/gg v1.3.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/gookit/color v1.4.2 // indirect
github.com/makeworld-the-better-one/dither/v2 v2.2.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/nathan-fiscaletti/consolesize-go v0.0.0-20210105204122-a87d9f614b9d // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
)
332 changes: 326 additions & 6 deletions go.sum

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,36 @@ func main() {
flag.Parse()
args := flag.Args()

object.StandardInput = os.Stdin
object.StandardOutput = os.Stdout
object.Arguments = args
object.ExitFunction = os.Exit

if len(os.Args) == 1 {
runRepl(os.Stdin, os.Stdout, args)
runRepl(os.Stdin, os.Stdout)
return
}

if len(*exec) > 0 {
execCode(*exec, os.Stdout, args)
execCode(*exec, os.Stdout)
return
}

file, err := ioutil.ReadFile(os.Args[1])
if err == nil {
execCode(string(file), os.Stdout, args)
execCode(string(file), os.Stdout)
return
}
}

func runRepl(in io.Reader, out io.Writer, args []string) {
replProgram := repl.NewRepel(out, in, args)
func runRepl(in io.Reader, out io.Writer) {
replProgram := repl.NewRepel(out, in)
replProgram.Version(version)

replProgram.Start()
}

func execCode(input string, writer io.Writer, args []string) {
object.Arguments = args
func execCode(input string, writer io.Writer) {
env := object.NewEnvironment()
l := lexer.New(strings.NewReader(input))
p := parser.New(l)
Expand Down
61 changes: 32 additions & 29 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package repl

import (
"bufio"
"fmt"
"github.com/TheZoraiz/ascii-image-converter/aic_package"
"io"
"ninja/evaluator"
"ninja/lexer"
Expand All @@ -13,26 +15,9 @@ import (
color "github.com/fatih/color"
)

const PROMPT = ">> "
const PROMPT = ">>> "

const NINJA_LICENSE = " License 2022 - Built by Jonathan Fontes - Version: %s"

const NINJA_SPLASH = `
$$\ $$\ $$\
$$$\ $$ |\__|
$$$$\ $$ |$$\ $$$$$$$\ $$\ $$$$$$\
$$ $$\$$ |$$ |$$ __$$\ \__| \____$$\
$$ \$$$$ |$$ |$$ | $$ |$$\ $$$$$$$ |
$$ |\$$$ |$$ |$$ | $$ |$$ |$$ __$$ |
$$ | \$$ |$$ |$$ | $$ |$$ |\$$$$$$$ |
\__| \__|\__|\__| \__|$$ | \_______|
$$\ $$ |
\$$$$$$ |
\______/
`
const NINJA_LICENSE = "Ninja Language - MIT LICENSE - Version: %s"

type repl struct {
out io.Writer
Expand All @@ -45,15 +30,11 @@ type repl struct {
var colorName = map[string]*color.Color{
"normal": color.New(color.FgWhite),
"program": color.New(color.FgWhite, color.Bold),
"brand": color.New(color.FgCyan, color.Bold),
"brand": color.New(color.FgHiBlue, color.Bold),
"error": color.New(color.FgRed),
}

func NewRepel(out io.Writer, in io.Reader, args []string) *repl {
object.StandardInput = in
object.StandardOutput = out
object.Arguments = args

func NewRepel(out io.Writer, in io.Reader) *repl {
scanner := bufio.NewScanner(in)
env := object.NewEnvironment()

Expand All @@ -79,11 +60,12 @@ func (r *repl) Start() {
panic(err)
}

r.printSplashLicense()
r.printSplashScreen()
r.printSplashLicense()

r.Output("normal", "Hi %s! This is Ninja Programming Language", user2.Username)
r.Output("normal", "Feel free to type in commands\n")
r.Output("program", "Hi %s! This is Ninja Programming Language\n", user2.Username)
r.Output("program", "Feel free to type in commands\n")
r.Output("program", "If found an error, open issue at github.com/gravataLonga/ninja\n")

for {
r.Output("normal", PROMPT)
Expand Down Expand Up @@ -122,7 +104,9 @@ func (r *repl) printSplashLicense() {
}

func (r *repl) printSplashScreen() {
r.Output("brand", NINJA_SPLASH)
fmt.Fprint(r.out, "\n\n")
fmt.Fprint(r.out, createSpashScreen())
fmt.Fprint(r.out, "\n\n")
}

func (r *repl) printParserErrors(errors []string) {
Expand All @@ -132,3 +116,22 @@ func (r *repl) printParserErrors(errors []string) {
r.Output("error", "\t\t"+msg+"\n")
}
}

func createSpashScreen() string {
filePath := "./docs/ninja-logo.png"

flags := aic_package.DefaultFlags()

flags.Width = 50
flags.Colored = true
flags.CustomMap = " .-+$*"
flags.SaveBackgroundColor = [4]int{50, 50, 50, 100}

// Conversion for an image
asciiArt, err := aic_package.Convert(filePath, flags)
if err != nil {
fmt.Println(err)
}

return fmt.Sprintf("%v\n", asciiArt)
}
24 changes: 24 additions & 0 deletions stdlib/exit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package stdlib

import "ninja/object"

// ExitFunction

// Exit execute exit function. Terminate following program
func Exit(args ...object.Object) object.Object {

err := object.Check(
"exit", args,
object.ExactArgs(1),
object.WithTypes(object.INTEGER_OBJ),
)

if err != nil {
return object.NewError(err.Error())
}

intV, _ := args[0].(*object.Integer)

object.ExitFunction(int(intV.Value))
return object.NULL
}
1 change: 1 addition & 0 deletions stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ var Builtins = map[string]*object.Builtin{
"time": object.NewBuiltin(Time),
"rand": object.NewBuiltin(Rand),
"args": object.NewBuiltin(Args),
"exit": object.NewBuiltin(Exit),
}

0 comments on commit 9a8d84b

Please sign in to comment.