Nicolas Winkler 6 年之前
父節點
當前提交
041cb132be
共有 5 個文件被更改,包括 73 次插入65 次删除
  1. 2 2
      src/CMakeLists.txt
  2. 13 5
      src/ast/AstVisitor.cpp
  3. 56 56
      src/ast/lexer.l
  4. 1 1
      src/test.qlw
  5. 1 1
      tests/runTests.py

+ 2 - 2
src/CMakeLists.txt

@@ -63,8 +63,8 @@ llvm_config(${PROJECT_NAME})
 
 
 llvm_map_components_to_libnames(llvm_libs X86 passes)
 llvm_map_components_to_libnames(llvm_libs X86 passes)
 
 
-message( ${llvm_libs} )
-target_link_libraries(${PROJECT_NAME} ${llvm_libs})
+#message( ${llvm_libs} )
+target_link_libraries(${PROJECT_NAME} LLVM) # ${llvm_libs})
 
 
 
 
 #    MIRParser
 #    MIRParser

+ 13 - 5
src/ast/AstVisitor.cpp

@@ -189,10 +189,10 @@ std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& a
         target = unique_dynamic_cast<sem::Expression>(
         target = unique_dynamic_cast<sem::Expression>(
             ast.target->accept(*this, scope));
             ast.target->accept(*this, scope));
     }
     }
-    
+
     sem::Method* method;
     sem::Method* method;
     sem::Variable* var;
     sem::Variable* var;
-    
+
     if (target) {
     if (target) {
         auto& targetType = scope.getContext().getType(target->type);
         auto& targetType = scope.getContext().getType(target->type);
         auto& typeScope = targetType.getTypeScope();
         auto& typeScope = targetType.getTypeScope();
@@ -203,7 +203,7 @@ std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& a
         method = scope.getMethod(ast.name);
         method = scope.getMethod(ast.name);
         var = scope.getVariable(ast.name);
         var = scope.getVariable(ast.name);
     }
     }
-    
+
     if (target) {
     if (target) {
         if (var) {
         if (var) {
             return std::make_unique<sem::FieldAccessExpression>(std::move(target), dynamic_cast<sem::Field*>(var));
             return std::make_unique<sem::FieldAccessExpression>(std::move(target), dynamic_cast<sem::Field*>(var));
@@ -211,7 +211,7 @@ std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& a
         else if (method) {
         else if (method) {
             auto fce = std::make_unique<sem::MethodCallExpression>(
             auto fce = std::make_unique<sem::MethodCallExpression>(
                 std::move(target), method);
                 std::move(target), method);
-    
+
             if (ast.arguments.size() != method->arguments.size())
             if (ast.arguments.size() != method->arguments.size())
                 throw SemanticError(SemanticError::WRONG_NUMBER_OF_ARGUMENTS, ast.name, ast.pos);
                 throw SemanticError(SemanticError::WRONG_NUMBER_OF_ARGUMENTS, ast.name, ast.pos);
             for (size_t i = 0; i < ast.arguments.size(); i++) {
             for (size_t i = 0; i < ast.arguments.size(); i++) {
@@ -253,7 +253,15 @@ std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& a
         }
         }
     }
     }
     else if (method) {
     else if (method) {
-        auto fce = std::make_unique<sem::MethodCallExpression>(nullptr, method);
+        // create implicit 'this'
+        std::unique_ptr<sem::Expression> thisExpr = nullptr;
+        if (method->containingClass != nullptr) {
+            auto* thisVar = scope.getVariable("this");
+            if (!thisVar)
+                throw SemanticError(SemanticError::UNKNOWN_TYPE, "no this found", ast.pos);
+            thisExpr = std::make_unique<sem::LocalVariableExpression>(thisVar);
+        }
+        auto fce = std::make_unique<sem::MethodCallExpression>(std::move(thisExpr), method);
         for (auto& arg : ast.arguments) {
         for (auto& arg : ast.arguments) {
             auto argument = arg->accept(*this, scope);
             auto argument = arg->accept(*this, scope);
             if (dynamic_cast<sem::Expression*>(argument.get())) {
             if (dynamic_cast<sem::Expression*>(argument.get())) {

+ 56 - 56
src/ast/lexer.l

@@ -36,8 +36,8 @@
 
 
 #define register
 #define register
 
 
-#define SET_TOKEN(t) (yylval_param->token = t)
-#define SET_STRING (yylval_param->string = new std::string(yytext, yyleng))
+#define CREATE_TOKEN(t) (yylval_param->token = t)
+#define CREATE_STRING (yylval_param->string = new std::string(yytext, yyleng))
 
 
 extern "C" int qlow_parser_wrap(yyscan_t s);
 extern "C" int qlow_parser_wrap(yyscan_t s);
 
 
@@ -76,12 +76,12 @@ UTF8CHAR [\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xe
 <COMMENT>\n             yycolumn = 0;
 <COMMENT>\n             yycolumn = 0;
 <COMMENT>.              ; // inside comment, ignore everything
 <COMMENT>.              ; // inside comment, ignore everything
 
 
-<LINE_COMMENT>"\n"      yycolumn = 0; yy_pop_state(yyscanner); //yy_push_state(INITIAL);
+<LINE_COMMENT>"\n"      yycolumn = 0; yy_pop_state(yyscanner); return CREATE_TOKEN(NEW_LINE); //yy_push_state(INITIAL);
 <LINE_COMMENT>.         ; // inside comment, ignore everything
 <LINE_COMMENT>.         ; // inside comment, ignore everything
 
 
 <STRING>"\""            yy_pop_state(yyscanner);
 <STRING>"\""            yy_pop_state(yyscanner);
 <STRING>[^\"^\n]*          printf("%s\n", std::string(yytext, yyleng).c_str());
 <STRING>[^\"^\n]*          printf("%s\n", std::string(yytext, yyleng).c_str());
-<STRING>\n              yycolumn = 0; SET_STRING; return UNEXPECTED_SYMBOL; 
+<STRING>\n              yycolumn = 0; CREATE_STRING; return UNEXPECTED_SYMBOL; 
 
 
 "/*"                    yy_push_state(COMMENT, yyscanner); commentDepth = 1;
 "/*"                    yy_push_state(COMMENT, yyscanner); commentDepth = 1;
 "//"                    yy_push_state(LINE_COMMENT, yyscanner);
 "//"                    yy_push_state(LINE_COMMENT, yyscanner);
@@ -89,61 +89,61 @@ UTF8CHAR [\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xe
 
 
 
 
 [\t ]                   ; // Space or tab ignored
 [\t ]                   ; // Space or tab ignored
-<METHOD>\n              yycolumn = 0; return SET_TOKEN(NEW_LINE);
-\n                      yycolumn = 0; //return SET_TOKEN(NEW_LINE);
-
-"class"                 return SET_TOKEN(CLASS);
-"struct"                return SET_TOKEN(STRUCT);
-"do"                    yy_push_state(METHOD, yyscanner); return SET_TOKEN(DO);
-<METHOD>"end"           yy_pop_state(yyscanner); return SET_TOKEN(END);
-<INITIAL>"end"          return SET_TOKEN(END);
-"if"                    return SET_TOKEN(IF);
-"while"                 return SET_TOKEN(WHILE);
-"else"                  return SET_TOKEN(ELSE);
-"return"                return SET_TOKEN(RETURN);
-"new"                   return SET_TOKEN(NEW);
-"extern"                return SET_TOKEN(EXTERN);
-"import"                return SET_TOKEN(IMPORT);
-
-":"                     return SET_TOKEN(COLON);
-";"                     return SET_TOKEN(SEMICOLON);
-","                     return SET_TOKEN(COMMA);
-"."                     return SET_TOKEN(DOT);
-"&"                     return SET_TOKEN(AMPERSAND);
-
-":="                    SET_STRING; return ASSIGN;
-
-"=="                    SET_STRING; return EQUALS;
-"!="                    SET_STRING; return NOT_EQUALS;
-"and"                   SET_STRING; return AND;
-"or"                    SET_STRING; return OR;
-"xor"                   SET_STRING; return XOR;
-"not"                   SET_STRING; return NOT;
-"as"                    return SET_TOKEN(AS);
-
-"+"                     SET_STRING; return PLUS;
-"-"                     SET_STRING; return MINUS;
-"*"                     SET_STRING; return ASTERISK;
-"/"                     SET_STRING; return SLASH;
-[\+\-\*\/=!<>]+         SET_STRING; return CUSTOM_OPERATOR;
-
-"("                     return SET_TOKEN(ROUND_LEFT);
-")"                     return SET_TOKEN(ROUND_RIGHT);
-"["                     return SET_TOKEN(SQUARE_LEFT);
-"]"                     return SET_TOKEN(SQUARE_RIGHT);
-
-"false"                 return SET_TOKEN(FALSE);
-"true"                  return SET_TOKEN(TRUE);
-
-[0-9_]+                 SET_STRING; return INT_LITERAL;
-0x[0-9A-Fa-f]+          SET_STRING; return INT_LITERAL;
-[a-zA-Z_][a-zA-Z0-9_]*  SET_STRING; return IDENTIFIER;
-
-.                       SET_STRING; return UNEXPECTED_SYMBOL; // printf("Unexpected symbol %s.\n", std::string(yytext, yyleng).c_str()); yyterminate();
+<METHOD>\n              yycolumn = 0; return CREATE_TOKEN(NEW_LINE);
+\n                      yycolumn = 0; //return CREATE_TOKEN(NEW_LINE);
+
+"class"                 return CREATE_TOKEN(CLASS);
+"struct"                return CREATE_TOKEN(STRUCT);
+"do"                    yy_push_state(METHOD, yyscanner); return CREATE_TOKEN(DO);
+<METHOD>"end"           yy_pop_state(yyscanner); return CREATE_TOKEN(END);
+<INITIAL>"end"          return CREATE_TOKEN(END);
+"if"                    return CREATE_TOKEN(IF);
+"while"                 return CREATE_TOKEN(WHILE);
+"else"                  return CREATE_TOKEN(ELSE);
+"return"                return CREATE_TOKEN(RETURN);
+"new"                   return CREATE_TOKEN(NEW);
+"extern"                return CREATE_TOKEN(EXTERN);
+"import"                return CREATE_TOKEN(IMPORT);
+
+":"                     return CREATE_TOKEN(COLON);
+";"                     return CREATE_TOKEN(SEMICOLON);
+","                     return CREATE_TOKEN(COMMA);
+"."                     return CREATE_TOKEN(DOT);
+"&"                     return CREATE_TOKEN(AMPERSAND);
+
+":="                    CREATE_STRING; return ASSIGN;
+
+"=="                    CREATE_STRING; return EQUALS;
+"!="                    CREATE_STRING; return NOT_EQUALS;
+"and"                   CREATE_STRING; return AND;
+"or"                    CREATE_STRING; return OR;
+"xor"                   CREATE_STRING; return XOR;
+"not"                   CREATE_STRING; return NOT;
+"as"                    return CREATE_TOKEN(AS);
+
+"+"                     CREATE_STRING; return PLUS;
+"-"                     CREATE_STRING; return MINUS;
+"*"                     CREATE_STRING; return ASTERISK;
+"/"                     CREATE_STRING; return SLASH;
+[\+\-\*\/=!<>]+         CREATE_STRING; return CUSTOM_OPERATOR;
+
+"("                     return CREATE_TOKEN(ROUND_LEFT);
+")"                     return CREATE_TOKEN(ROUND_RIGHT);
+"["                     return CREATE_TOKEN(SQUARE_LEFT);
+"]"                     return CREATE_TOKEN(SQUARE_RIGHT);
+
+"false"                 return CREATE_TOKEN(FALSE);
+"true"                  return CREATE_TOKEN(TRUE);
+
+[0-9_]+                 CREATE_STRING; return INT_LITERAL;
+0x[0-9A-Fa-f]+          CREATE_STRING; return INT_LITERAL;
+[a-zA-Z_][a-zA-Z0-9_]*  CREATE_STRING; return IDENTIFIER;
+
+.                       CREATE_STRING; return UNEXPECTED_SYMBOL; // printf("Unexpected symbol %s.\n", std::string(yytext, yyleng).c_str()); yyterminate();
 
 
 %%
 %%
 
 
-// [a-zA-Z_][a-zA-Z0-9_]*  SET_STRING; return IDENTIFIER;
+// [a-zA-Z_][a-zA-Z0-9_]*  CREATE_STRING; return IDENTIFIER;
 
 
 
 
 /*
 /*

+ 1 - 1
src/test.qlw

@@ -4,7 +4,7 @@ fast_fibonacci(i: Integer): Integer do
     temp: Integer
     temp: Integer
     count: Integer
     count: Integer
     count := i 
     count := i 
-    a := 0 // asdfgadf
+    a := 0 // sdfsfaf
     b := 1
     b := 1
     while count != 0 do
     while count != 0 do
         temp := a
         temp := a

+ 1 - 1
tests/runTests.py

@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 
 import sys
 import sys
 import os
 import os