Skip to content

Commit

Permalink
[mycpp] Generate a globally unique name for fmtX() functions.
Browse files Browse the repository at this point in the history
Also fix some format strings issues.

26 compile errors in osh_eval.
  • Loading branch information
Andy Chu committed Feb 27, 2020
1 parent c567114 commit 731ccf6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
7 changes: 4 additions & 3 deletions bin/osh_eval.py
Expand Up @@ -82,14 +82,15 @@ def Eval(self, node):
log('arg %s', arg)
for w in node.words:
val = self.word_ev.EvalWordToString(w)
# TODO: tagswitch
log('arg %s', val)
# TODO: how to print repr() in C++?
log('arg %d', val.tag_())

elif case(command_e.DParen):
node = cast(command__DParen, UP_node)

a = self.arith_ev.Eval(node.child)
log('arith val %s', a)
# TODO: how to print repr() in C++?
log('arith val %d', a.tag_())

else:
log('Unhandled node %s', command_str(node.tag_()))
Expand Down
14 changes: 11 additions & 3 deletions core/ui.py
Expand Up @@ -15,14 +15,14 @@
from _devbuild.gen.syntax_asdl import (
Token, command_t, command,
source_e, source__Stdin, source__MainFile, source__SourcedFile,
source__EvalArg, source__Alias, source__LValue
source__EvalArg, source__Alias, source__LValue,
)
from _devbuild.gen.runtime_asdl import value_e, value_t, value__Str
from _devbuild.gen.runtime_asdl import value_str, value_e, value_t, value__Str
from asdl import runtime
from asdl import format as fmt
from osh import word_
from mycpp import mylib
from mycpp.mylib import tagswitch
from mycpp.mylib import tagswitch, NewStr

from typing import List, cast, Any, TYPE_CHECKING
if TYPE_CHECKING:
Expand All @@ -32,6 +32,14 @@
#from frontend.args import UsageError


def ValType(val):
# type: (value_t) -> str
"""For displaying type errors in the UI."""

# Displays 'value.MaybeStrArray' for now, maybe change it.
return NewStr(value_str(val.tag_()))


def PrettyToken(tok, arena):
# type: (Token, Arena) -> str
"""Returns a readable token value for the user. For syntax errors."""
Expand Down
4 changes: 2 additions & 2 deletions doc/simple-word-eval.md
Expand Up @@ -258,8 +258,8 @@ Oil can express everything that shell can.

- [nullglob]($help) - Globs matching nothing don't evaluate to code.
- [dashglob]($help) is true by default, but **disabled** when Oil is enabled, so that
files that begin with `-` aren't returned. This avoids confusing **flags and
files**.
files that begin with `-` aren't returned. This avoids [confusing flags and
files](https://www.oilshell.org/blog/2020/02/dashglob.html).

Strict options cause fatal errors:

Expand Down
4 changes: 2 additions & 2 deletions mycpp/cppgen_pass.py
Expand Up @@ -612,8 +612,8 @@ def _WriteFmtFunc(self, fmt, fmt_types):
Returns:
the temp fmtX() name we used.
"""
temp_name = 'fmt%d' % self.unique_id
self.unique_id += 1
temp_name = 'fmt%d' % self.fmt_ids['_counter']
self.fmt_ids['_counter'] += 1

fmt_parts = format_strings.Parse(fmt)
self.fmt_funcs.write('Str* %s(' % temp_name)
Expand Down
5 changes: 4 additions & 1 deletion mycpp/mycpp_main.py
Expand Up @@ -203,7 +203,10 @@ def main(argv):
#log('V %s', virtual.virtuals)

local_vars = {} # FuncDef node -> (name, c_type) list
fmt_ids = {} # Node -> fmt_name

# Node -> fmt_name, plus a hack for the counter
# TODO: This could be a class with 2 members
fmt_ids = {'_counter': 0}

# First generate ALL C++ declarations / "headers".
# class Foo { void method(); }; class Bar { void method(); };
Expand Down
8 changes: 5 additions & 3 deletions osh/sh_expr_eval.py
Expand Up @@ -36,6 +36,7 @@
from frontend import match
from osh import bool_stat
from core import state
from core import ui
from osh import word_

from mycpp import mylib
Expand Down Expand Up @@ -382,8 +383,8 @@ def _ValToIntOrError(self, val, span_id=runtime.NO_SPID):
# Arrays and associative arrays always fail -- not controlled by strict_arith.
# In bash, (( a )) is like (( a[0] )), but I don't want that.
# And returning '0' gives different results.
e_die("Expected a value convertible to integer, got %s", val,
span_id=span_id)
e_die("Expected a value convertible to integer, got %s",
ui.ValType(val), span_id=span_id)

def _EvalLhsAndLookupArith(self, node):
# type: (sh_lhs_expr_t) -> Tuple[int, lvalue_t]
Expand Down Expand Up @@ -595,7 +596,8 @@ def Eval(self, node):

else:
# TODO: Add error context
e_die('Expected array or assoc in index expression, got %s', left)
e_die('Expected array or assoc in index expression, got %s',
ui.ValType(left))

if s is None:
val = value.Undef() # type: value_t
Expand Down

0 comments on commit 731ccf6

Please sign in to comment.