Compare commits
3 commits
2c354c4a96
...
5c336c7dec
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c336c7dec | |||
| ddf92e1009 | |||
| e2a1e5ff8d |
4 changed files with 53 additions and 19 deletions
|
|
@ -195,6 +195,34 @@ func (c *Compiler) compile(tree Node) error {
|
||||||
|
|
||||||
switch tree.Type() {
|
switch tree.Type() {
|
||||||
case StringNodeType:
|
case StringNodeType:
|
||||||
|
n := tree.(*StringNode)
|
||||||
|
s := ""
|
||||||
|
escaped := false
|
||||||
|
|
||||||
|
for _, ch := range n.value {
|
||||||
|
if escaped {
|
||||||
|
switch ch {
|
||||||
|
case 'n':
|
||||||
|
s += "\n"
|
||||||
|
case 't':
|
||||||
|
s += "\t"
|
||||||
|
case 'r':
|
||||||
|
s += "\r"
|
||||||
|
default:
|
||||||
|
s += string(ch)
|
||||||
|
}
|
||||||
|
escaped = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ch {
|
||||||
|
case '\\':
|
||||||
|
escaped = true
|
||||||
|
default:
|
||||||
|
s += string(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c.add(InstructionConstant)
|
c.add(InstructionConstant)
|
||||||
c.addConstant(&StringValue{
|
c.addConstant(&StringValue{
|
||||||
tree.(*StringNode).value,
|
tree.(*StringNode).value,
|
||||||
|
|
|
||||||
|
|
@ -1419,31 +1419,14 @@ func (p *Parser) parseSignature() (TypeSignature, error) {
|
||||||
name := (*p.prev).Lexeme
|
name := (*p.prev).Lexeme
|
||||||
|
|
||||||
switch name {
|
switch name {
|
||||||
case "string":
|
case "str":
|
||||||
s = &StringSignature{}
|
s = &StringSignature{}
|
||||||
case "int":
|
case "int":
|
||||||
s = &IntegerSignature{}
|
s = &IntegerSignature{}
|
||||||
case "float":
|
case "float":
|
||||||
s = &FloatSignature{}
|
s = &FloatSignature{}
|
||||||
case "boolean":
|
case "bool":
|
||||||
s = &BooleanSignature{}
|
s = &BooleanSignature{}
|
||||||
case "list":
|
|
||||||
if err := p.expect(TokenOpenBracket, "list must have content typed"); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
contents, err := p.parseSignature()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := p.expect(TokenCloseBracket, "square brackets enclose list content signature"); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
s = &ListSignature{
|
|
||||||
contents,
|
|
||||||
}
|
|
||||||
|
|
||||||
case "any":
|
case "any":
|
||||||
s = &AnySignature{}
|
s = &AnySignature{}
|
||||||
|
|
|
||||||
20
core/vm.go
20
core/vm.go
|
|
@ -470,6 +470,26 @@ var DefaultGlobals = map[string]Value{
|
||||||
nil,
|
nil,
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
"assert": &BuiltinFunctionValue{
|
||||||
|
"assert",
|
||||||
|
&FunctionSignature{
|
||||||
|
[]TypeSignature{
|
||||||
|
&BooleanSignature{},
|
||||||
|
},
|
||||||
|
&NilSignature{},
|
||||||
|
},
|
||||||
|
func(vm *VM, this Value, params []Value) (Value, error) {
|
||||||
|
b := params[0].(*BoolValue)
|
||||||
|
|
||||||
|
if !b.Boolean {
|
||||||
|
return nil, errors.New(fmt.Sprintf("assertion failed: %s", b))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &NilValue{}, nil
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
false,
|
||||||
|
},
|
||||||
"assertEq": &BuiltinFunctionValue{
|
"assertEq": &BuiltinFunctionValue{
|
||||||
"assertEq",
|
"assertEq",
|
||||||
&FunctionSignature{
|
&FunctionSignature{
|
||||||
|
|
|
||||||
|
|
@ -8,3 +8,6 @@ assertEq([3, 1, 4, 1], [3, 1, 4, 1])
|
||||||
|
|
||||||
# Inequality
|
# Inequality
|
||||||
assertNotEq(2, 3)
|
assertNotEq(2, 3)
|
||||||
|
|
||||||
|
assert(1 < 2)
|
||||||
|
assert(1.0 < 2.0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue