Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
Add support for converting string to num using '+' prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
haifenghuang committed Aug 9, 2018
1 parent b01e37b commit f954ebe
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Table of Contents
* [Useful Utilities](#useful-utilities)
* [Document generator](#document-generator)
* [Syntax Highlight](#syntax-highlight)
* [Futual Plans](#futual-plans)
* [Future Plans](#future-plans)
* [License](#license)

## Summary
Expand Down Expand Up @@ -1088,6 +1088,20 @@ revStr = reverse(str)
println("Reverse str =", revStr)
```

If you hava a string, you want to convert it to number, you could add a "+" prefix before the string.

```swfit
a = +"121314" // a is an int
println(a) // result: 121314
// Integer also support "0x"(hex), "0b"(binary), "0c"(octal) prefix
a = +"0x10" // a is an int
println(a) // result: 16
a = +"121314.6789" // a is a float
println(a) // result: 121314.6789
```

### Hash
In monkey, the builtin hash will keep the order of keys when they are added to the hash, just like python's orderedDict.

Expand Down
14 changes: 14 additions & 0 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,20 @@ revStr = reverse(str)
println("Reverse str =", revStr)
```

如果你希望将一个包含数字的字符串转换为数字,你可以在字符串之前加入"+“号,将此字符串转换为数字:

```swfit
a = +"121314" // a是一个整数
println(a) // 结果:121314
// 整数支持"0x"(十六进制), "0b"(二进制), "0c"(八进制)前缀
a = +"0x10" // a是一个整数
println(a) // 结果:16
a = +"121314.6789" // a是一个浮点数
println(a) // 结果:121314.6789
```

### 哈希(Hash)
在monkey中,哈希默认会保持Key的插入顺序,类似Python的orderedDict.

Expand Down
36 changes: 35 additions & 1 deletion src/monkey/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"regexp"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"unicode/utf8"
Expand Down Expand Up @@ -1427,7 +1428,40 @@ func evalPrefixExpression(p *ast.PrefixExpression, scope *Scope) Object {
case "!":
return evalBangOperatorExpression(right)
case "+":
return right
switch right.Type() {
case STRING_OBJ:
var n int64
var err error

var content = right.(*String).String
if content[len(content)-1] == 'u' {
content = content[:len(content)-1]
}

if strings.HasPrefix(content, "0b") {
n, err = strconv.ParseInt(content[2:], 2, 64)
} else if strings.HasPrefix(content, "0x") {
n, err = strconv.ParseInt(content[2:], 16, 64)
} else if strings.HasPrefix(content, "0c") {
n, err = strconv.ParseInt(content[2:], 8, 64)
} else {
n, err = strconv.ParseInt(content, 10, 64)
if err != nil {
// Check if it is a float
var f float64
f, err = strconv.ParseFloat(content, 64)
if err == nil {
return NewFloat(f)
}
}
}
if err != nil {
panic(NewError(p.Pos().Sline(), PREFIXOP, p, right.Type()))
}
return NewInteger(n)
default:
return right
}
case "-":
switch right.Type() {
case INTEGER_OBJ:
Expand Down

0 comments on commit f954ebe

Please sign in to comment.