diff options
-rwxr-xr-x | compiler/parser.nim | 9 | ||||
-rwxr-xr-x | compiler/types.nim | 3 | ||||
-rwxr-xr-x | doc/manual.txt | 16 | ||||
-rwxr-xr-x | tests/compile/tobjects.nim | 10 | ||||
-rwxr-xr-x | todo.txt | 1 |
5 files changed, 35 insertions, 4 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim index d1042b04d..df31a7df8 100755 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -1279,7 +1279,13 @@ proc parseObjectCase(p: var TParser): PNode = addSon(a, parseTypeDesc(p)) addSon(a, ast.emptyNode) addSon(result, a) + if p.tok.tokType == tkColon: getTok(p) skipComment(p, result) + var wasIndented = false + if p.tok.tokType == tkInd: + pushInd(p.lex, p.tok.indent) + getTok(p) + wasIndented = true while true: if p.tok.tokType == tkSad: getTok(p) var b: PNode @@ -1300,6 +1306,9 @@ proc parseObjectCase(p: var TParser): PNode = addSon(b, fields) addSon(result, b) if b.kind == nkElse: break + if wasIndented: + eat(p, tkDed) + popInd(p.lex) proc parseObjectPart(p: var TParser): PNode = case p.tok.tokType diff --git a/compiler/types.nim b/compiler/types.nim index fb0e9a123..0919b7c9e 100755 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -97,7 +97,8 @@ proc getOrdValue(n: PNode): biggestInt = case n.kind of nkCharLit..nkInt64Lit: result = n.intVal of nkNilLit: result = 0 - else: + of nkHiddenStdConv: result = getOrdValue(n.sons[1]) + else: LocalError(n.info, errOrdinalTypeExpected) result = 0 diff --git a/doc/manual.txt b/doc/manual.txt index bf50c9679..1641f5338 100755 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -932,7 +932,10 @@ An example: As can been seen from the example, an advantage to an object hierarchy is that no casting between different object types is needed. Yet, access to invalid object fields raises an exception. - + +The syntax of ``case`` in an object declaration follows closely the syntax of +the ``case`` statement: The branches in a ``case`` section may be indented too. + Set type ~~~~~~~~ @@ -1732,7 +1735,16 @@ Example: of "delete-everything", "restart-computer": echo("permission denied") of "go-for-a-walk": echo("please yourself") - else: echo("unknown command") + else: echo("unknown command") + + # indentation of the branches is also allowed; and so is an optional colon + # after the selecting expression: + case readline(stdin): + of "delete-everything", "restart-computer": + echo("permission denied") + of "go-for-a-walk": echo("please yourself") + else: echo("unknown command") + The `case`:idx: statement is similar to the if statement, but it represents a multi-branch selection. The expression after the keyword ``case`` is diff --git a/tests/compile/tobjects.nim b/tests/compile/tobjects.nim index 8305e2838..40abae312 100755 --- a/tests/compile/tobjects.nim +++ b/tests/compile/tobjects.nim @@ -11,6 +11,16 @@ type d, e, f: char else: nil n: bool + +type + TMyObject = object of TObject + case disp: range[0..4]: + of 0: arg: char + of 1: s: string + else: wtf: bool + +var + x: TMyObject var global: int diff --git a/todo.txt b/todo.txt index 3fc3a6484..f77d4d2d4 100755 --- a/todo.txt +++ b/todo.txt @@ -11,7 +11,6 @@ New pragmas: - implement ``byCopy`` pragma - ``borrow`` needs to take type classes into account -- complete and document optional indentation for 'case' statement - make templates hygienic by default: try to gensym() everything in the 'block' of a template; find a better solution for gensym instead of `*ident` - ``bind`` for overloaded symbols does not work apparently |