@@ -129,7 +129,8 @@ Parser2::checkType(const std::string& t0, const std::string& t1)
129129 // should actually check types here
130130
131131 // edge case lists
132- if (left.at (0 ) == ' [' || right.at (0 ) == ' [' )
132+ if (left.size () > 0 && right.size () > 0 &&
133+ (left.at (0 ) == ' [' || right.at (0 ) == ' [' ))
133134 {
134135 // if one is a list, both must be lists
135136 if (left.at (0 ) == ' [' )
@@ -196,7 +197,8 @@ Parser2::checkExists(const std::string& s)
196197 if (s.at (0 ) == ' [' )
197198 type = extractListType (type);
198199
199- if (!symboltable->hasVariable (type) && !isPrimitive (type))
200+ if (!symboltable->hasVariable (type) && !isPrimitive (type) &&
201+ !udttable->hasUDT (type))
200202 semanticerrorhandler->handle (std::make_unique<Error2>(Error2 (
201203 currentToken->col , currentToken->line , " Unknown variable or type." ,
202204 " Unknown variable/type named: " , type, " ." )));
@@ -358,14 +360,41 @@ Parser2::parseProgram()
358360{
359361 parseSource ();
360362}
363+
364+ bool
365+ containsUDT (const std::string& filename)
366+ {
367+ auto lex = std::make_unique<Lexar2>(filename, true );
368+ auto currentToken = lex->getNextToken ();
369+ while (currentToken->kind != TokenKind::EOF_)
370+ {
371+ if (currentToken->kind == TokenKind::UAT)
372+ return true ;
373+ currentToken = lex->getNextToken ();
374+ }
375+
376+ return false ;
377+ }
378+
361379/* *
362380 * Source := Import Source | SourcePart
363381 */
364382void
365383Parser2::parseSource ()
366384{
367- recursiveParse (false , TokenKind::IMPORT,
368- [this ]() { this ->parseImportInfo (); });
385+ if (containsUDT (filename))
386+ isUdt = true ;
387+
388+ if (!isUdt)
389+ recursiveParse (false , TokenKind::IMPORT,
390+ [this ]() { this ->parseImportInfo (); });
391+ else
392+ {
393+ if (currentToken->kind == TokenKind::IMPORT)
394+ semanticerrorhandler->handle (std::make_unique<Error2>(
395+ Error2 (currentToken->col , currentToken->line ,
396+ " Illegal import in udt file." , " " , " " , " " )));
397+ }
369398 parseSourcePart ();
370399}
371400
@@ -386,9 +415,9 @@ Parser2::parseImportInfo()
386415
387416 auto file = loc.substr (1 , loc.size () - 2 );
388417
418+ std::cout << " Compiling import: " + file + " \n " ;
389419 try
390420 {
391- std::cout << " Compiling import: " + file + " \n " ;
392421 auto udtFlagAndBufer = parseFile (file);
393422
394423 auto table = std::move (std::get<0 >(udtFlagAndBufer));
@@ -409,14 +438,14 @@ Parser2::parseImportInfo()
409438 " Received: " , name,
410439 " and expected: " + extractUDTName (file) + " ." )));
411440
412- // add to own udt table under imported name, throwing an error if the
413- // name already exists
441+ // add to own udt table under imported name, throwing an error if
442+ // the name already exists
414443 if (!udttable->hasUDT (name))
415444 udttable->addUDT (
416445 name, table->getAttributeSymbolTable (extractUDTName (file)),
417446 table->getMethodSymbolTable (extractUDTName (file)));
418447
419- symboltable->addSymbol (name , " U" );
448+ symboltable->addSymbol (extractUDTName (file) , " U" );
420449
421450 // aggregate udt buffers
422451 targetBuffer += buf;
@@ -458,7 +487,6 @@ Parser2::parseSourcePart()
458487 switch (currentToken->kind )
459488 {
460489 case TokenKind::UAT:
461- isUdt = true ;
462490 parseUDT ();
463491 break ;
464492 default :
@@ -507,10 +535,10 @@ Parser2::parseUserDefinedType()
507535 " \n //___________BEGIN_" + udtname + " _UDT_DEFINITION__________/_//\n\n " ;
508536
509537 targetBuffer += " struct " + udtname + " \n {\n " ;
510- a_st = this ->parseAttributes (a_st);
538+ this ->parseAttributes (a_st);
511539 targetBuffer += " };\n " ;
512540
513- std::shared_ptr<SymbolTable> methodst = this ->parseMethods (m_st);
541+ this ->parseMethods (m_st);
514542
515543 targetBuffer +=
516544 " //___________END_" + udtname + " _UDT_DEFINITION__________/_//\n\n " ;
@@ -523,7 +551,7 @@ Parser2::parseUserDefinedType()
523551 * - type exists
524552 * - unique attribute
525553 */
526- std::shared_ptr<SymbolTable>
554+ void
527555Parser2::parseAttributes (std::shared_ptr<SymbolTable> st)
528556{
529557 advanceAndCheckToken (TokenKind::UAT); // consume uat
@@ -539,7 +567,9 @@ Parser2::parseAttributes(std::shared_ptr<SymbolTable> st)
539567
540568 auto outtype = type;
541569 if ((st->hasVariable (outtype) && st->getSymbolType (outtype) == " U" ) ||
542- udttable->hasUDT (outtype))
570+ udttable->hasUDT (outtype) ||
571+ (symboltable->hasVariable (outtype) &&
572+ symboltable->getSymbolType (outtype) == " U" ))
543573 outtype = " struct " + outtype + " *" ;
544574 else
545575 outtype = builtinTypesTranslator (outtype);
@@ -569,7 +599,7 @@ Parser2::parseAttributes(std::shared_ptr<SymbolTable> st)
569599 }
570600
571601 if (!st->hasVariable (type) && !isPrimitive (type) &&
572- !udttable->hasUDT (type))
602+ !udttable->hasUDT (type) && !symboltable-> hasVariable (type) )
573603 {
574604 semanticerrorhandler->handle (std::make_unique<Error2>(
575605 Error2 (currentToken->col , currentToken->line ,
@@ -590,14 +620,12 @@ Parser2::parseAttributes(std::shared_ptr<SymbolTable> st)
590620 st->removeSymbol (extractUDTName (filename));
591621
592622 advanceAndCheckToken (TokenKind::RCURLEY); // consume r curley
593-
594- return st;
595623}
596624
597625/* *
598626 * Methods := 'Ufn' [FunctionDefinition]*
599627 */
600- std::shared_ptr<SymbolTable>
628+ void
601629Parser2::parseMethods (std::shared_ptr<SymbolTable> st)
602630{
603631 advanceAndCheckToken (TokenKind::UFN); // consume ufn
@@ -618,7 +646,6 @@ Parser2::parseMethods(std::shared_ptr<SymbolTable> st)
618646 symboltable = temp;
619647
620648 advanceAndCheckToken (TokenKind::RCURLEY); // consume r curley
621- return st;
622649}
623650
624651void
@@ -740,17 +767,8 @@ Parser2::parseFunctionInOut(const std::string& name)
740767 }
741768
742769 if (type != " void" )
743- {
744- auto ok = symboltable->addSymbol (std::get<0 >(sands), type);
745- // if (!ok)
746- // semanticerrorhandler->handle(std::make_unique<Error2>(
747- // Error2(currentToken->col, currentToken->line,
748- // "Unexpected redeclaration of " + name +
749- // ", originally defined as type " +
750- // symboltable->getSymbolType(name) + ".",
751- // "Received second declaration of type: ", type,
752- // ".")));
753- }
770+ symboltable->addSymbol (std::get<0 >(sands), type);
771+
754772 types += " _" + type;
755773 });
756774 advanceAndCheckToken (TokenKind::RPAREN); // consume r paren
@@ -1500,7 +1518,6 @@ Parser2::parseNew()
15001518std::string
15011519Parser2::parseUDTDec ()
15021520{
1503-
15041521 auto udtName = parseIdentifier ();
15051522 targetBuffer +=
15061523 " (struct " + udtName + " *)malloc(sizeof(struct " + udtName + " ));\n " ;
0 commit comments