-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved run to main and fixed binary parsing bug
- Loading branch information
Showing
12 changed files
with
138 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/jesperkha/Fizz | ||
|
||
go 1.16 | ||
|
||
require github.com/daviddengcn/go-colortext v1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
github.com/daviddengcn/go-colortext v1.0.0 h1:ANqDyC0ys6qCSvuEK7l3g5RaehL/Xck9EX8ATG8oKsE= | ||
github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c= | ||
github.com/golangplus/bytes v0.0.0-20160111154220-45c989fe5450/go.mod h1:Bk6SMAONeMXrxql8uvOKuAZSu8aM5RUGv+1C6IJaEho= | ||
github.com/golangplus/bytes v1.0.0/go.mod h1:AdRaCFwmc/00ZzELMWb01soso6W1R/++O1XL80yAn+A= | ||
github.com/golangplus/fmt v1.0.0/go.mod h1:zpM0OfbMCjPtd2qkTD/jX2MgiFCqklhSUFyDW44gVQE= | ||
github.com/golangplus/testing v1.0.0 h1:+ZeeiKZENNOMkTTELoSySazi+XaEhVO0mb+eanrSEUQ= | ||
github.com/golangplus/testing v1.0.0/go.mod h1:ZDreixUV3YzhoVraIDyOzHrr76p6NUh6k/pPg/Q3gYA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package run | ||
package main | ||
|
||
import ( | ||
"github.com/jesperkha/Fizz/lexer" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
ct "github.com/daviddengcn/go-colortext" | ||
) | ||
|
||
var ( | ||
ErrFileNotFound = errors.New("cannot find file with name: '%s'") | ||
ErrNonFizzFile = errors.New("cannot run non-Fizz file") | ||
) | ||
|
||
var cmdOptions = map[string]func(){ | ||
"help": func() { | ||
fmt.Println("use: fizz [filename.fizz | --option]") | ||
}, | ||
} | ||
|
||
func RunInterpeter(args []string) { | ||
if len(args) == 2 { | ||
arg := args[1] | ||
|
||
// Run option commands | ||
if strings.HasPrefix(arg, "--") { | ||
option, ok := cmdOptions[strings.TrimLeft(arg, "-")] | ||
if !ok { | ||
formatError(fmt.Errorf("unknown option: '%s'", arg)) | ||
return | ||
} | ||
|
||
option() | ||
} | ||
|
||
// Run fizz file | ||
filename := args[1] | ||
if !strings.HasSuffix(filename, ".fizz") { | ||
filename += ".fizz" | ||
} | ||
|
||
if err := runFile(filename); err != nil { | ||
formatError(err) | ||
} | ||
|
||
return | ||
} | ||
|
||
// Run terminal mode | ||
runTerminal() | ||
} | ||
|
||
// Prints errors with red color to terminal | ||
func formatError(err error) { | ||
ct.Foreground(ct.Red, true) | ||
fmt.Println(err.Error()) | ||
ct.ResetColor() | ||
} | ||
|
||
// Leaves the interpreter running as the user inputs code to the terminal. | ||
// Prints out errors but does not terminate until ^C or 'exit'. | ||
func runTerminal() { | ||
scanner := bufio.NewScanner(os.Stdin) | ||
totalString := "" | ||
numBlocks := 0 | ||
indent := " " | ||
|
||
fmt.Println("type 'exit' to terminate session") | ||
for { | ||
fmt.Print("::: " + strings.Repeat(indent, numBlocks)) | ||
scanner.Scan() | ||
input := scanner.Text() | ||
|
||
if input == "exit" { | ||
break | ||
} | ||
|
||
// Continue with indent after braces | ||
numBlocks += strings.Count(input, "{") - strings.Count(input, "}") | ||
totalString += input + "\n" // Better error handling | ||
|
||
if numBlocks <= 0 { | ||
err := Interperate(totalString) | ||
if err != nil { | ||
formatError(err) | ||
} | ||
|
||
totalString = "" | ||
numBlocks = 0 | ||
} | ||
} | ||
|
||
fmt.Println("session ended") | ||
} | ||
|
||
// Interperates code found in file specified in commandline arguments | ||
func runFile(filename string) (err error) { | ||
if !strings.HasSuffix(filename, ".fizz") { | ||
return ErrNonFizzFile | ||
} | ||
|
||
if file, err := os.Open(filename); err == nil { | ||
var buf bytes.Buffer | ||
bufio.NewReader(file).WriteTo(&buf) | ||
return Interperate(buf.String()) | ||
} | ||
|
||
// Assumes path error | ||
return fmt.Errorf(ErrFileNotFound.Error(), filename) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters