Bläddra i källkod

support line continuation character

blueloveTH 2 år sedan
förälder
incheckning
31850d3f29
2 ändrade filer med 38 tillägg och 0 borttagningar
  1. 7 0
      src/lexer.cpp
  2. 31 0
      tests/04_line_continue.py

+ 7 - 0
src/lexer.cpp

@@ -315,6 +315,13 @@ static bool is_unicode_Lo_char(uint32_t c) {
                 case '[': add_token(TK("[")); return true;
                 case ']': add_token(TK("]")); return true;
                 case '@': add_token(TK("@")); return true;
+                case '\\': {
+                    // line continuation character
+                    char c = eatchar_include_newline();
+                    if (c != '\n') SyntaxError("expected newline after line continuation character");
+                    eat_spaces();
+                    return true;
+                }
                 case '$': {
                     for(int i=TK("$goto"); i<=TK("$label"); i++){
                         // +1 to skip the '$'

+ 31 - 0
tests/04_line_continue.py

@@ -0,0 +1,31 @@
+a = 1 + 2 \
+    + 3
+
+assert a == 6
+
+assert 1 + 2 \
+    + 3 == 6
+
+assert 1 + 2 + \
+    3 + \
+    4 == 10
+
+assert 1 + 2 + \
+    3 + \
+    4 + 5 + 6 \
+    == 21
+
+if 1 and 2 \
+    and 3 \
+    and 4 \
+    and 5:
+    assert True
+else:
+    assert False
+
+1 and 2 \
+    and 3 \
+    and 4
+
+a = 1
+assert a == 1