Skip to content

Commit

Permalink
[oil-language] Parse null / true / false
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Sep 13, 2019
1 parent 44f6660 commit 5d08e7c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions core/id_kind.py
Expand Up @@ -384,11 +384,15 @@ def AddKinds(spec):

'NotTilde', # !~

# TODO: We could use the same Dummy for all?
'WordsDummy', # Used for @() (words in lex_mode_e.ShCommand)
'CommandDummy', # Used for $() (command in lex_mode_e.ShCommand)
'DqDummy', # Used for ""
'SqDummy', # Used for ''

# Constants
'Null', 'True', 'False',

# Keywords are resolved after lexing, but otherwise behave like tokens.
# NOTE: These are not used because pgen2 automatically creates a
# gr.keywords dict in the grammar.
Expand Down
6 changes: 6 additions & 0 deletions frontend/lex.py
Expand Up @@ -822,6 +822,12 @@ def IsKeyword(name):

# NOTE: Borrowing tokens from Arith (i.e. $(( )) ), but not using LexerPairs().
LEXER_DEF[lex_mode_e.Expr] = _OIL_LEFT_SUBS + _OIL_LEFT_UNQUOTED + [
# NOTE: pgen2 is taking care of 'in', 'is', etc.? Should we register those?
# We probably want those too.
C('null', Id.Expr_Null),
C('true', Id.Expr_True),
C('false', Id.Expr_False),

# These can be looked up as keywords separately, so you enforce that they have
# space around them?
R(VAR_NAME_RE, Id.Expr_Name),
Expand Down
10 changes: 9 additions & 1 deletion oil_lang/expr_eval.py
Expand Up @@ -93,7 +93,15 @@ def EvalExpr(self, node):
print('')

if node.tag == expr_e.Const:
return int(node.c.val)
id_ = node.c.id
if id_ == Id.Expr_Digits:
return int(node.c.val)
elif id_ == Id.Expr_Null:
return None
elif id_ == Id.Expr_True:
return True
elif id_ == Id.Expr_False:
return False

if node.tag == expr_e.Var:
return self.LookupVar(node.name.val)
Expand Down
3 changes: 3 additions & 0 deletions oil_lang/expr_to_ast.py
Expand Up @@ -316,8 +316,11 @@ def Expr(self, pnode):

if tok.id == Id.Expr_Name:
return expr.Var(tok)
# TODO: Should I combine all of these?
elif tok.id == Id.Expr_Digits:
return expr.Const(tok)
elif tok.id in (Id.Expr_Null, Id.Expr_True, Id.Expr_False):
return expr.Const(tok)

else:
raise AssertionError(tok.id)
Expand Down
2 changes: 1 addition & 1 deletion oil_lang/grammar.pgen2
Expand Up @@ -113,7 +113,7 @@ atom_expr: ['await'] atom trailer*
atom: ('(' [yield_expr|testlist_comp] ')' |
'[' [testlist_comp] ']' |
'{' [dictorsetmaker] '}' |
NAME | NUMBER | '...' | 'None' | 'True' | 'False' |
NAME | NUMBER | '...' | Expr_Null | Expr_True | Expr_False |
array_literal | sh_array_literal |
command_sub | sh_command_sub |
regex_literal |
Expand Down
26 changes: 26 additions & 0 deletions spec/oil-expr.test.sh
Expand Up @@ -367,3 +367,29 @@ bar
foo
bar
## END

#### null / true / false
shopt -s all:oil
var n = null
if (n) {
echo yes
} else {
echo no
}
var t = true
if (t) {
echo yes
} else {
echo no
}
var f = false
if (f) {
echo yes
} else {
echo no
}
## STDOUT:
no
yes
no
## END

0 comments on commit 5d08e7c

Please sign in to comment.