| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* Copyright 2025 Jonathan S. Arney | ||
| 2 | * | ||
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | * you may not use this file except in compliance with the License. | ||
| 5 | * You may obtain a copy of the License at | ||
| 6 | * | ||
| 7 | * https://github.com/jarney/gyoji/blob/master/LICENSE | ||
| 8 | * | ||
| 9 | * Unless required by applicable law or agreed to in writing, software | ||
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | * See the License for the specific language governing permissions and | ||
| 13 | * limitations under the License. | ||
| 14 | */ | ||
| 15 | #include <gyoji-frontend.hpp> | ||
| 16 | |||
| 17 | #include <gyoji.l.hpp> | ||
| 18 | #include <gyoji.y.hpp> | ||
| 19 | |||
| 20 | using namespace Gyoji::context; | ||
| 21 | using namespace Gyoji::mir; | ||
| 22 | using namespace Gyoji::frontend; | ||
| 23 | using namespace Gyoji::frontend::ast; | ||
| 24 | using namespace Gyoji::frontend::tree; | ||
| 25 | using namespace Gyoji::frontend::lowering; | ||
| 26 | using namespace Gyoji::frontend::namespaces; | ||
| 27 | using namespace Gyoji::frontend::yacc; | ||
| 28 | |||
| 29 | Gyoji::owned<ParseResult> | ||
| 30 | 240 | Parser::parse( | |
| 31 | Gyoji::context::CompilerContext & _compiler_context, | ||
| 32 | Gyoji::misc::InputSource & _input_source | ||
| 33 | ) | ||
| 34 | { | ||
| 35 | 240 | auto ns2_context = std::make_unique<Gyoji::frontend::namespaces::NS2Context>(); | |
| 36 | Gyoji::owned<ParseResult> result = std::make_unique<ParseResult>( | ||
| 37 | _compiler_context, | ||
| 38 | 240 | std::move(ns2_context) | |
| 39 | 240 | ); | |
| 40 | |||
| 41 | yyscan_t scanner; | ||
| 42 | 240 | yylex_init(&scanner); | |
| 43 | |||
| 44 | LexContext lex_context( | ||
| 45 | 240 | *result->ns2_context, | |
| 46 | _compiler_context, | ||
| 47 | 240 | _input_source); | |
| 48 | 240 | yyset_extra(&lex_context, scanner); | |
| 49 | |||
| 50 | 240 | yacc::YaccParser parser { scanner, *result }; | |
| 51 | 240 | parser.parse(); | |
| 52 | 240 | yylex_destroy(scanner); | |
| 53 | |||
| 54 | 240 | return result; | |
| 55 | 240 | } | |
| 56 | |||
| 57 | Gyoji::owned<MIR> | ||
| 58 | 30 | Parser::parse_to_mir( | |
| 59 | Gyoji::context::CompilerContext & _compiler_context, | ||
| 60 | Gyoji::misc::InputSource & _input_source | ||
| 61 | ) | ||
| 62 | { | ||
| 63 | |||
| 64 | // We don't need to report an error at this point | ||
| 65 | // because lack of a translation unit means | ||
| 66 | // that our caller should not even have called us | ||
| 67 | // and should report a syntax error at the | ||
| 68 | // higher level. | ||
| 69 | 30 | Gyoji::owned<ParseResult> parse_result = parse(_compiler_context, _input_source); | |
| 70 | 30 | Gyoji::owned<MIR> mir = std::make_unique<MIR>(); | |
| 71 | |||
| 72 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
30 | if (!parse_result->has_translation_unit()) { |
| 73 | // It's harmless to return an empty mir | ||
| 74 | // to the next stages | ||
| 75 | ✗ | return mir; | |
| 76 | } | ||
| 77 | |||
| 78 | 30 | Gyoji::mir::operation_static_init(); | |
| 79 | |||
| 80 | 30 | fprintf(stderr, "============================\n"); | |
| 81 | 30 | fprintf(stderr, "Type and symbol table resolution pass\n"); | |
| 82 | 30 | fprintf(stderr, "============================\n"); | |
| 83 | // First, resolve all of the type definitions. | ||
| 84 | // Also at this stage, we resolve the function declarations. | ||
| 85 | TypeLowering type_lowering(_compiler_context, | ||
| 86 | parse_result->get_translation_unit(), | ||
| 87 | 30 | *mir); | |
| 88 | 30 | type_lowering.lower(); | |
| 89 | |||
| 90 | 30 | fprintf(stderr, "============================\n"); | |
| 91 | 30 | fprintf(stderr, "Function resolution pass\n"); | |
| 92 | 30 | fprintf(stderr, "============================\n"); | |
| 93 | FunctionLowering function_lowering(_compiler_context, | ||
| 94 | 30 | *parse_result, | |
| 95 | 30 | *mir, | |
| 96 | 30 | type_lowering); | |
| 97 | 30 | function_lowering.lower(); | |
| 98 | |||
| 99 | 30 | return mir; | |
| 100 | 30 | } | |
| 101 |