Skip to content

Commit

Permalink
Lint + documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
thesephist committed Jan 5, 2020
1 parent f9ae6d0 commit d81beaa
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 95 deletions.
10 changes: 5 additions & 5 deletions cmd/ink.go
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/thesephist/ink/pkg/ink"
)

const Version = "0.1.7"
const cliVersion = "0.1.7"

const HelpMessage = `
const helpMessage = `
Ink is a minimal, powerful, functional programming language.
ink v%s
Expand All @@ -32,7 +32,7 @@ Run from the command line with -eval.

func main() {
flag.Usage = func() {
fmt.Printf(HelpMessage, Version)
fmt.Printf(helpMessage, cliVersion)
flag.PrintDefaults()
}

Expand Down Expand Up @@ -62,7 +62,7 @@ func main() {

// if asked for version, disregard everything else
if *version {
fmt.Printf("ink v%s\n", Version)
fmt.Printf("ink v%s\n", cliVersion)
os.Exit(0)
} else if *help {
flag.Usage()
Expand Down Expand Up @@ -103,7 +103,7 @@ func main() {

for {
// green arrow
fmt.Printf(ink.ANSI_GREEN_BOLD + "> " + ink.ANSI_RESET)
fmt.Printf(ink.AnsiGreenBold + "> " + ink.AnsiReset)
text, err := reader.ReadString('\n')

if err == io.EOF {
Expand Down
116 changes: 58 additions & 58 deletions pkg/ink/eval.go
Expand Up @@ -11,7 +11,7 @@ import (
"sync"
)

const MaxPrintLen = 120
const maxPrintLen = 120

// Value represents any value in the Ink programming language.
// Each value corresponds to some primitive or object value created
Expand All @@ -35,9 +35,9 @@ func isIntable(n NumberValue) bool {
func nToS(f float64) string {
if i := int64(f); f == float64(i) {
return strconv.FormatInt(i, 10)
} else {
return strconv.FormatFloat(f, 'f', 8, 64)
}

return strconv.FormatFloat(f, 'f', 8, 64)
}

// nToS for NumberValue type
Expand Down Expand Up @@ -72,9 +72,9 @@ func (v NumberValue) Equals(other Value) bool {

if ov, ok := other.(NumberValue); ok {
return v == ov
} else {
return false
}

return false
}

// StringValue represents all characters and strings in Ink
Expand All @@ -93,9 +93,9 @@ func (v StringValue) Equals(other Value) bool {

if ov, ok := other.(StringValue); ok {
return bytes.Equal(v, ov)
} else {
return false
}

return false
}

// BooleanValue is either `true` or `false`
Expand All @@ -116,9 +116,9 @@ func (v BooleanValue) Equals(other Value) bool {

if ov, ok := other.(BooleanValue); ok {
return v == ov
} else {
return false
}

return false
}

// NullValue is a value that only exists at the type level,
Expand Down Expand Up @@ -166,9 +166,9 @@ func (v CompositeValue) Equals(other Value) bool {
}
}
return true
} else {
return false
}

return false
}

// FunctionValue is the value of any variables referencing functions
Expand All @@ -182,8 +182,8 @@ func (v FunctionValue) String() string {
// ellipsize function body at a reasonable length,
// so as not to be too verbose in repl environments
fstr := v.defn.String()
if len(fstr) > MaxPrintLen {
fstr = fstr[:MaxPrintLen] + ".."
if len(fstr) > maxPrintLen {
fstr = fstr[:maxPrintLen] + ".."
}
return fstr
}
Expand All @@ -197,9 +197,9 @@ func (v FunctionValue) Equals(other Value) bool {
// to compare structs containing slices, we really want
// a pointer comparison, not a value comparison
return v.defn == ov.defn
} else {
return false
}

return false
}

// FunctionCallThunkValue is an internal representation of a lazy
Expand All @@ -222,9 +222,9 @@ func (v FunctionCallThunkValue) Equals(other Value) bool {
// to compare structs containing slices, we really want
// a pointer comparison, not a value comparison
return &v.vt == &ov.vt && &v.function == &ov.function
} else {
return false
}

return false
}

// unwrapThunk expands out a recursive structure of thunks
Expand Down Expand Up @@ -408,12 +408,12 @@ func (n BinaryExprNode) Eval(frame *StackFrame, allowThunk bool) (Value, error)
left, err := n.leftOperand.Eval(frame, false)
if err != nil {
return nil, err
} else {
return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot assign value to non-identifier %s [%s]",
left, poss(n.leftOperand)),
}
}

return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot assign value to non-identifier %s [%s]",
left, poss(n.leftOperand)),
}
}
} else if n.operator == AccessorOp {
Expand All @@ -431,9 +431,9 @@ func (n BinaryExprNode) Eval(frame *StackFrame, allowThunk bool) (Value, error)
v, prs := leftValueComposite[rightValueStr]
if prs {
return v, nil
} else {
return NullValue{}, nil
}

return NullValue{}, nil
} else if leftString, isString := leftValue.(StringValue); isString {
rightNum, err := strconv.ParseInt(rightValueStr, 10, 64)
if err != nil {
Expand All @@ -447,9 +447,9 @@ func (n BinaryExprNode) Eval(frame *StackFrame, allowThunk bool) (Value, error)
rn := int(rightNum)
if -1 < rn && rn < len(leftString) {
return StringValue([]byte{leftString[rn]}), nil
} else {
return NullValue{}, nil
}

return NullValue{}, nil
} else {
return nil, Err{
ErrRuntime,
Expand Down Expand Up @@ -530,9 +530,9 @@ func (n BinaryExprNode) Eval(frame *StackFrame, allowThunk bool) (Value, error)
ErrRuntime,
fmt.Sprintf("division by zero error [%s]", poss(n.rightOperand)),
}
} else {
return leftNum / right, nil
}

return leftNum / right, nil
}
}
return nil, Err{
Expand All @@ -552,12 +552,12 @@ func (n BinaryExprNode) Eval(frame *StackFrame, allowThunk bool) (Value, error)

if isIntable(right) {
return NumberValue(int(leftNum) % int(right)), nil
} else {
return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot take modulus of non-integer value %s [%s]",
nvToS(right), poss(n.leftOperand)),
}
}

return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot take modulus of non-integer value %s [%s]",
nvToS(right), poss(n.leftOperand)),
}
}
}
Expand All @@ -571,12 +571,12 @@ func (n BinaryExprNode) Eval(frame *StackFrame, allowThunk bool) (Value, error)
if rightNum, ok := rightValue.(NumberValue); ok {
if isIntable(leftNum) && isIntable(rightNum) {
return NumberValue(int64(leftNum) & int64(rightNum)), nil
} else {
return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot take bitwise & of non-integer values %s, %s [%s]",
nvToS(rightNum), nvToS(leftNum), poss(n)),
}
}

return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot take bitwise & of non-integer values %s, %s [%s]",
nvToS(rightNum), nvToS(leftNum), poss(n)),
}
}
} else if leftBool, isBool := leftValue.(BooleanValue); isBool {
Expand All @@ -594,12 +594,12 @@ func (n BinaryExprNode) Eval(frame *StackFrame, allowThunk bool) (Value, error)
if rightNum, ok := rightValue.(NumberValue); ok {
if isIntable(leftNum) && isIntable(rightNum) {
return NumberValue(int64(leftNum) | int64(rightNum)), nil
} else {
return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot take bitwise | of non-integer values %s, %s [%s]",
nvToS(rightNum), nvToS(leftNum), poss(n)),
}
}

return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot take bitwise | of non-integer values %s, %s [%s]",
nvToS(rightNum), nvToS(leftNum), poss(n)),
}
}
} else if leftBool, isBool := leftValue.(BooleanValue); isBool {
Expand All @@ -617,12 +617,12 @@ func (n BinaryExprNode) Eval(frame *StackFrame, allowThunk bool) (Value, error)
if rightNum, ok := rightValue.(NumberValue); ok {
if isIntable(leftNum) && isIntable(rightNum) {
return NumberValue(int64(leftNum) ^ int64(rightNum)), nil
} else {
return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot take logical & of non-integer values %s, %s [%s]",
nvToS(rightNum), nvToS(leftNum), poss(n)),
}
}

return nil, Err{
ErrRuntime,
fmt.Sprintf("cannot take logical & of non-integer values %s, %s [%s]",
nvToS(rightNum), nvToS(leftNum), poss(n)),
}
}
} else if leftBool, isBool := leftValue.(BooleanValue); isBool {
Expand Down Expand Up @@ -710,11 +710,11 @@ func evalInkFunction(fn Value, allowThunk bool, args ...Value) (Value, error) {
vt: argValueTable,
function: fnt,
}

if allowThunk {
return returnThunk, nil
} else {
return unwrapThunk(returnThunk)
}
return unwrapThunk(returnThunk)
} else if fnt, isNativeFunc := fn.(NativeFunctionValue); isNativeFunc {
return fnt.exec(fnt.ctx, args)
} else {
Expand Down Expand Up @@ -878,7 +878,7 @@ func (frame *StackFrame) Set(name string, val Value) {
frame.vt[name] = val
}

// Update a value in the stack frame chain
// Up updates a value in the stack frame chain
func (frame *StackFrame) Up(name string, val Value) {
for frame != nil {
_, ok := frame.vt[name]
Expand All @@ -901,8 +901,8 @@ func (frame *StackFrame) String() string {
entries := make([]string, 0, len(frame.vt))
for k, v := range frame.vt {
vstr := v.String()
if len(vstr) > MaxPrintLen {
vstr = vstr[:MaxPrintLen] + ".."
if len(vstr) > maxPrintLen {
vstr = vstr[:maxPrintLen] + ".."
}
entries = append(entries, fmt.Sprintf("%s -> %s", k, vstr))
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/ink/lexer.go
Expand Up @@ -8,6 +8,8 @@ import (
"unicode"
)

// Kind is the sum type of all possible types
// of tokens in an Ink program
type Kind int

const (
Expand Down
26 changes: 14 additions & 12 deletions pkg/ink/log.go
Expand Up @@ -7,33 +7,35 @@ import (
)

const (
ANSI_RESET = ""
ANSI_BLUE = ""
ANSI_GREEN = ""
ANSI_YELLOW = ""
ANSI_RED = ""
ANSI_BLUE_BOLD = ""
ANSI_GREEN_BOLD = ""
ANSI_YELLOW_BOLD = ""
ANSI_RED_BOLD = ""
// ANSI terminal escape codes for color output
AnsiReset = ""
AnsiBlue = ""
AnsiGreen = ""
ansiYellow = ""
AnsiRed = ""
AnsiBlueBold = ""
AnsiGreenBold = ""
AnsiYellowBold = ""
AnsiRedBold = ""
)

func LogDebug(args ...string) {
fmt.Println(ANSI_BLUE_BOLD + "debug: " + ANSI_BLUE + strings.Join(args, " ") + ANSI_RESET)
fmt.Println(AnsiBlueBold + "debug: " + AnsiBlue + strings.Join(args, " ") + AnsiReset)
}

func LogDebugf(s string, args ...interface{}) {
LogDebug(fmt.Sprintf(s, args...))
}

func LogInteractive(args ...string) {
fmt.Println(ANSI_GREEN + strings.Join(args, " ") + ANSI_RESET)
fmt.Println(AnsiGreen + strings.Join(args, " ") + AnsiReset)
}

func LogInteractivef(s string, args ...interface{}) {
LogInteractive(fmt.Sprintf(s, args...))
}

// LogSafeErr is like LogErr, but does not immediately exit the interpreter
func LogSafeErr(reason int, args ...string) {
errStr := "error"
switch reason {
Expand All @@ -48,7 +50,7 @@ func LogSafeErr(reason int, args ...string) {
default:
errStr = "error"
}
fmt.Fprintln(os.Stderr, ANSI_RED_BOLD+errStr+": "+ANSI_RED+strings.Join(args, " ")+ANSI_RESET)
fmt.Fprintln(os.Stderr, AnsiRedBold+errStr+": "+AnsiRed+strings.Join(args, " ")+AnsiReset)
}

func LogErr(reason int, args ...string) {
Expand Down

0 comments on commit d81beaa

Please sign in to comment.