Fix a few flubs

* Add a TODO as a reminder to implement number toString formatting.

* Change the Loreley delimiters to something obscure ("<{%" and "%}>")
  to avoid conflicting with actual characters we might use in messages.
  Also, make the assertions more descriptive should Loreley fail.

* Rip out a debug.PrintStack() in verbose logging.

* Check the underlying pointer's object type for +=, not the pointer type.
This commit is contained in:
joeduffy 2017-02-16 04:15:07 -08:00
parent b16590d6c3
commit 6dcdf9e884
3 changed files with 29 additions and 13 deletions

View file

@ -20,6 +20,8 @@ export function toString(argument: Object): string {
return "false";
}
if (isNumber(argument)) {
// TODO: implement number formatting.
return "NaN";
}
if (isSymbol(argument)) {
throw new TypeError();

View file

@ -106,6 +106,19 @@ func (d *defaultSink) Warningf(diag *Diag, args ...interface{}) {
d.warnings++
}
const colorLeft = "<{%"
const colorRight = "%}>"
func init() {
// Change the Loreley delimiters from { and }, to something more complex, to avoid accidental collisions.
loreley.DelimLeft = colorLeft
loreley.DelimRight = colorRight
}
func colorString(s string) string {
return colorLeft + s + colorRight
}
func (d *defaultSink) Stringify(diag *Diag, cat Category, args ...interface{}) string {
var buffer bytes.Buffer
@ -119,9 +132,9 @@ func (d *defaultSink) Stringify(diag *Diag, cat Category, args ...interface{}) s
if d.opts.Colors {
switch cat {
case Error:
buffer.WriteString("{fg 1}") // red
buffer.WriteString(colorString("fg 1")) // red
case Warning:
buffer.WriteString("{fg 11}") // bright yellow
buffer.WriteString(colorString("fg 11")) // bright yellow
default:
contract.Failf("Unrecognized diagnostic category: %v", cat)
}
@ -138,18 +151,18 @@ func (d *defaultSink) Stringify(diag *Diag, cat Category, args ...interface{}) s
buffer.WriteString(": ")
if d.opts.Colors {
buffer.WriteString("{reset}")
buffer.WriteString(colorString("reset"))
}
// Finally, actually print the message itself.
if d.opts.Colors {
buffer.WriteString("{fg 7}") // white
buffer.WriteString(colorString("fg 7")) // white
}
buffer.WriteString(fmt.Sprintf(diag.Message, args...))
if d.opts.Colors {
buffer.WriteString("{reset}")
buffer.WriteString(colorString("reset"))
}
buffer.WriteRune('\n')
@ -163,7 +176,7 @@ func (d *defaultSink) Stringify(diag *Diag, cat Category, args ...interface{}) s
if d.opts.Colors {
var err error
s, err = loreley.CompileAndExecuteToString(s, nil, nil)
contract.Assert(err == nil)
contract.Assertf(err == nil, "Expected no errors during string format operation; str=%v, err=%v", s, err)
}
return s
@ -174,7 +187,7 @@ func (d *defaultSink) StringifyLocation(doc *Document, loc *Location) string {
if doc != nil {
if d.opts.Colors {
buffer.WriteString("{fg 6}") // cyan
buffer.WriteString(colorString("fg 6")) // cyan
}
file := doc.File
@ -199,7 +212,7 @@ func (d *defaultSink) StringifyLocation(doc *Document, loc *Location) string {
var s string
if doc != nil || loc != nil {
if d.opts.Colors {
buffer.WriteString("{reset}")
buffer.WriteString(colorString("reset"))
}
s = buffer.String()
@ -208,7 +221,7 @@ func (d *defaultSink) StringifyLocation(doc *Document, loc *Location) string {
if d.opts.Colors {
var err error
s, err = loreley.CompileAndExecuteToString(s, nil, nil)
contract.Assert(err == nil)
contract.Assertf(err == nil, "Expected no errors during string format operation; str=%v, err=%v", s, err)
}
}

View file

@ -5,7 +5,6 @@ package eval
import (
"math"
"reflect"
"runtime/debug"
"sort"
"strconv"
@ -569,7 +568,7 @@ func (e *evaluator) evalCall(node diag.Diagable, fnc symbols.Function,
if uw != nil {
if uw.Throw() {
if glog.V(7) {
glog.V(7).Infof("Evaluated call to fnc %v; unhandled exception: %v", uw.Exception().Thrown)
glog.V(7).Infof("Evaluated call to fnc %v; unhandled exception: %v", fnc, uw.Exception().Thrown)
}
return nil, uw
}
@ -654,6 +653,9 @@ func (e *evaluator) evalLocalVariableDeclaration(node *ast.LocalVariableDeclarat
if node.Local.Default != nil {
obj := e.alloc.NewConstant(*node.Local.Default)
e.locals.SetValue(sym, obj)
if e.hooks != nil {
e.hooks.OnVariableAssign(nil, tokens.Name(sym.Name()), nil, obj)
}
}
return nil
@ -807,7 +809,6 @@ func (e *evaluator) evalExpressionStatement(node *ast.ExpressionStatement) *rt.U
func (e *evaluator) evalExpression(node ast.Expression) (*rt.Object, *rt.Unwind) {
if glog.V(7) {
glog.V(7).Infof("Evaluating expression: %v", reflect.TypeOf(node))
debug.PrintStack()
}
// Simply switch on the node type and dispatch to the specific function, returning the object and rt.Unwind info.
@ -1479,7 +1480,7 @@ func (e *evaluator) evalBinaryOperatorExpression(node *ast.BinaryOperatorExpress
case ast.OpAssignSum:
var val *rt.Object
ptr := lhs.PointerValue()
if lhs.Type() == types.String {
if ptr.Obj().Type() == types.String {
// If the lhs/rhs are strings, just concatenate += and yield the new value as a result.
val = e.alloc.NewString(ptr.Obj().StringValue() + rhs.StringValue())
} else {