Skip to content

Commit

Permalink
[oil-language] Implement 1_000_000 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy C committed Jul 5, 2021
1 parent 1590590 commit 06919a3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
14 changes: 7 additions & 7 deletions doc/oil-language-tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ If statements have optional `elif` and `else` clauses:

if test --file foo {
echo 'foo is a file'
rm --verbose foo # delete it
rm --verbose foo # delete it
} elif test --dir foo {
echo 'foo is a directory'
} else {
Expand All @@ -522,7 +522,7 @@ If statements have optional `elif` and `else` clauses:

If statements are also used for **error handling**:

if ! cp foo /tmp { # The word ! inverts the exit status
if ! cp foo /tmp { # The word ! inverts the exit status
echo 'error copying'
}

Expand All @@ -540,7 +540,7 @@ rather than a command:
}

var done = false
if (not done) { # negate with 'not' operator (contrast with !)
if (not done) { # negate with 'not' operator (contrast with !)
echo "we aren't done"
}

Expand Down Expand Up @@ -652,11 +652,11 @@ breaks the rule that types are spelled with capital letters (e.g. `Str`,

There are many ways to write integers:

#var small, big = 42, 65_536 # TODO: _ not supported yet
#echo "$small $big" # => 42 65536
var small, big = 42, 65_536
echo "$small $big" # => 42 65536

var hex, octal, binary = 0xFF, 0o755, 0b0101
echo "$hex $octal $binary" # => 255 493 5
var hex, octal, binary = 0x0001_0000, 0o755, 0b0001_0101
echo "$hex $octal $binary" # => 65536 493 21

<!--
TODO: not supported yet
Expand Down
14 changes: 9 additions & 5 deletions oil_lang/expr_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,21 @@ def EvalExpr(self, node):
if node.tag == expr_e.Const:
id_ = node.c.id

# Remove underscores from 1_000_000. The lexer is responsible for
# validation.
c = node.c.val.replace('_', '')

if id_ == Id.Expr_DecInt:
return int(node.c.val)
return int(c)
elif id_ == Id.Expr_BinInt:
return int(node.c.val, 2)
return int(c, 2)
elif id_ == Id.Expr_OctInt:
return int(node.c.val, 8)
return int(c, 8)
elif id_ == Id.Expr_HexInt:
return int(node.c.val, 16)
return int(c, 16)

elif id_ == Id.Expr_Float:
return float(node.c.val)
return float(c)

elif id_ == Id.Expr_Null:
return None
Expand Down
43 changes: 43 additions & 0 deletions spec/oil-expr.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,49 @@ echo $d $b $o $h
123 3 83 255
## END

#### Integer literals with underscores
const dec = 65_536
const bin = 0b0001_0101
const oct = 0o001_755
const hex = 0x0001_000f

echo SHELL
echo $dec
echo $bin
echo $oct
echo $hex
const x = 1_1 + 0b1_1 + 0o1_1 + 0x1_1
echo sum $x

echo ---
echo PYTHON

python3 -c '
print(65_536)
print(0b0001_0101)
print(0o001_755)
print(0x0001_000f)
# Weird syntax
print("sum", 1_1 + 0b1_1 + 0o1_1 + 0x1_1)
'

## STDOUT:
SHELL
65536
21
1005
65551
sum 40
---
PYTHON
65536
21
1005
65551
sum 40
## END

#### Float Literals
shopt -s oil:basic
# 1+2 2.3
Expand Down

0 comments on commit 06919a3

Please sign in to comment.