summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-04-23 17:11:24 +0200
committerAraq <rumpf_a@web.de>2011-04-23 17:11:24 +0200
commit4ba4999bb7b172b683cf7b8d574adbf04afa7527 (patch)
treef1216df68dba7abb0e9324afa11464db885800dd /compiler
parent05fee773ec48b5b45cdb7469a6c8410b59fcb542 (diff)
downloadNim-4ba4999bb7b172b683cf7b8d574adbf04afa7527.tar.gz
slice support in system.nim; syntactic sugar for tables; cleanup of grammar/parser
Diffstat (limited to 'compiler')
-rwxr-xr-xcompiler/parser.nim72
-rwxr-xr-xcompiler/semexprs.nim67
2 files changed, 51 insertions, 88 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim
index 2289bb98f..1b8f2f259 100755
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -173,49 +173,30 @@ proc parseSymbol(p: var TParser): PNode =
   of tkAccent: 
     result = newNodeP(nkAccQuoted, p)
     getTok(p)
-    case p.tok.tokType
-    of tkBracketLe: 
-      var s = "["
-      getTok(p)
-      while true:
-        if p.tok.tokType == tkComma:
-          add(s, ",")
-          getTok(p)
-        elif (p.tok.tokType == tkOpr) and (p.tok.ident.s == "$"): 
-          add(s, "$..")
-          getTok(p)
-          eat(p, tkDotDot)
-          if (p.tok.tokType == tkOpr) and (p.tok.ident.s == "$"): 
-            add(s, '$')
-            getTok(p)
-        elif p.tok.tokType == tkDotDot: 
-          add(s, "..")
-          getTok(p)
-          if (p.tok.tokType == tkOpr) and (p.tok.ident.s == "$"): 
-            add(s, '$')
-            getTok(p)
-        else: break
-      eat(p, tkBracketRi)
-      add(s, ']')
-      if p.tok.tokType == tkEquals: 
-        add(s, '=')
+    var id = ""
+    while true:
+      case p.tok.tokType
+      of tkBracketLe: 
+        id.add("[]")
         getTok(p)
-      addSon(result, newIdentNodeP(getIdent(s), p))
-    of tkParLe: 
-      addSon(result, newIdentNodeP(getIdent("()"), p))
-      getTok(p)
-      eat(p, tkParRi)
-    of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDotDot: 
-      var id = p.tok.ident
-      getTok(p)
-      if p.tok.tokType == tkEquals: 
-        addSon(result, newIdentNodeP(getIdent(id.s & '='), p))
+        eat(p, tkBracketRi)
+      of tkEquals:
+        id.add('=')
         getTok(p)
-      else: 
-        addSon(result, newIdentNodeP(id, p))
-    else: 
-      parMessage(p, errIdentifierExpected, tokToStr(p.tok))
-      result = ast.emptyNode
+      of tkParLe: 
+        id.add("()")
+        getTok(p)
+        eat(p, tkParRi)
+      of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDotDot:
+        id.add(p.tok.ident.s)
+        getTok(p)
+      of tkIntLit..tkCharLit:
+        id.add(tokToStr(p.tok))
+        getTok(p)
+      else:
+        if id.len == 0: parMessage(p, errIdentifierExpected, tokToStr(p.tok))
+        break
+    add(result, newIdentNodeP(getIdent(id), p))
     eat(p, tkAccent)
   else: 
     parMessage(p, errIdentifierExpected, tokToStr(p.tok))
@@ -247,15 +228,8 @@ proc accExpr(p: var TParser): PNode =
   eat(p, tkAccent)
 
 proc indexExpr(p: var TParser): PNode = 
-  # indexExpr ::= expr ['=' expr]
   result = parseExpr(p)
-  if p.tok.tokType == tkEquals: 
-    var a = result
-    result = newNodeP(nkExprEqExpr, p)
-    addSon(result, a)
-    getTok(p)
-    addSon(result, parseExpr(p))
-  
+
 proc indexExprList(p: var TParser, first: PNode): PNode = 
   result = newNodeP(nkBracketExpr, p)
   addSon(result, first)
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 56de998f5..35fb793c4 100755
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -709,41 +709,11 @@ proc semFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
     else: 
       GlobalError(n.Info, errUndeclaredFieldX, i.s)
 
-proc whichSliceOpr(n: PNode): string = 
-  if n.sons[0].kind == nkEmpty: 
-    if n.sons[1].kind == nkEmpty: result = "[..]"
-    else: result = "[..$]"
-  elif n.sons[1].kind == nkEmpty: 
-    result = "[$..]"
-  else: 
-    result = "[$..$]"
-
-proc addSliceOpr(result: var string, n: PNode) = 
-  if n[0].kind == nkEmpty: 
-    if n[1].kind == nkEmpty: result.add("..")
-    else: result.add("..$")
-  elif n[1].kind == nkEmpty: result.add("$..") 
-  else: result.add("$..$")
-
 proc buildOverloadedSubscripts(n: PNode, inAsgn: bool): PNode =
   result = newNodeI(nkCall, n.info)
-  add(result, ast.emptyNode) # fill with the correct node later
-  add(result, n[0])
-  var opr = "["
-  for i in 1..n.len-1:
-    if i > 1: add(opr, ",")
-    if n[i].kind == nkRange:
-      # we have a slice argument
-      checkSonsLen(n[i], 2)
-      addSliceOpr(opr, n[i])
-      addSon(result, n[i][0])
-      addSon(result, n[i][1])
-    else:
-      add(result, n[i])
-  if inAsgn: add(opr, "]=")
-  else: add(opr, "]")
-  # now we know the operator
-  result.sons[0] = newIdentNode(getIdent(opr), n.info)
+  result.add(newIdentNode(
+    if inAsgn: getIdent"[]=" else: getIdent"[]", n.info))
+  for i in 0 .. n.len-1: result.add(n[i])
   
 proc semDeref(c: PContext, n: PNode): PNode =
   checkSonsLen(n, 1)
@@ -773,10 +743,11 @@ proc semSubscript(c: PContext, n: PNode, flags: TExprFlags): PNode =
       n.sons[i] = semExprWithType(c, n.sons[i], flags - {efAllowType})
     var indexType = if arr.kind == tyArray: arr.sons[0] else: getSysType(tyInt)
     var arg = IndexTypesMatch(c, indexType, n.sons[1].typ, n.sons[1])
-    if arg != nil: n.sons[1] = arg
-    else: GlobalError(n.info, errIndexTypesDoNotMatch)
-    result = n
-    result.typ = elemType(arr)
+    if arg != nil: 
+      n.sons[1] = arg
+      result = n
+      result.typ = elemType(arr)
+    #GlobalError(n.info, errIndexTypesDoNotMatch)
   of tyTuple: 
     checkSonsLen(n, 2)
     n.sons[0] = makeDeref(n.sons[0])
@@ -862,6 +833,21 @@ proc semSetConstr(c: PContext, n: PNode): PNode =
         m = fitNode(c, typ, n.sons[i])
       addSon(result, m)
 
+proc semTableConstr(c: PContext, n: PNode): PNode = 
+  # we simply transform ``{key: value, key2: value}`` to 
+  # ``[(key, value), (key2, value2)]``
+  result = newNodeI(nkBracket, n.info)
+  for i in 0..n.len-1:
+    var x = n.sons[i]
+    if x.kind == nkExprColonExpr and sonsLen(x) == 2:
+      var pair = newNodeI(nkPar, x.info)
+      pair.add(x[0])
+      pair.add(x[1])
+      result.add(pair)
+    else:
+      illFormedAst(x)
+  result = semExpr(c, result)
+
 type 
   TParKind = enum 
     paNone, paSingle, paTupleFields, paTuplePositions
@@ -967,7 +953,8 @@ proc semMacroStmt(c: PContext, n: PNode, semCheck = true): PNode =
       result = semTemplateExpr(c, result, s, semCheck)
     else: GlobalError(n.info, errXisNoMacroOrTemplate, s.name.s)
   else: 
-    GlobalError(n.info, errInvalidExpressionX, renderTree(a, {renderNoComments}))
+    GlobalError(n.info, errInvalidExpressionX, 
+                renderTree(a, {renderNoComments}))
   
 proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode = 
   result = n
@@ -1090,7 +1077,9 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
     checkSonsLen(n, 3)
   of nkCheckedFieldExpr: 
     checkMinSonsLen(n, 2)
-  of nkSymChoice: 
+  of nkTableConstr:
+    result = semTableConstr(c, n)
+  of nkSymChoice:
     GlobalError(n.info, errExprXAmbiguous, renderTree(n, {renderNoComments}))
   else:
     GlobalError(n.info, errInvalidExpressionX,