Start using driver, and switch to file IO.

This commit is contained in:
Danila Fedorin 2020-09-09 13:00:06 -07:00
parent 2f09401685
commit a6839c548e
4 changed files with 80 additions and 17 deletions

View File

@ -8,6 +8,7 @@
#include "parser.hpp"
#include "error.hpp"
#include "type.hpp"
#include "parse_driver.hpp"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
@ -21,8 +22,6 @@ void yy::parser::error(const yy::location& loc, const std::string& msg) {
std::cout << "An error occured: " << msg << std::endl;
}
extern definition_group global_defs;
void typecheck_program(
definition_group& defs,
type_mgr& mgr, type_env_ptr& env) {
@ -131,13 +130,17 @@ void gen_llvm(global_scope& scope) {
output_llvm(ctx, "program.o");
}
int main() {
yy::parser parser;
int main(int argc, char** argv) {
if(argc != 2) {
std::cout << "please enter a file to compile." << std::endl;
}
parse_driver driver(argv[1]);
driver.run_parse();
type_mgr mgr;
type_env_ptr env(new type_env);
parser.parse();
for(auto& def_defn : global_defs.defs_defn) {
for(auto& def_defn : driver.global_defs.defs_defn) {
std::cout << def_defn.second->name;
for(auto& param : def_defn.second->params) std::cout << " " << param;
std::cout << ":" << std::endl;
@ -145,8 +148,8 @@ int main() {
def_defn.second->body->print(1, std::cout);
}
try {
typecheck_program(global_defs, mgr, env);
global_scope scope = translate_program(global_defs);
typecheck_program(driver.global_defs, mgr, env);
global_scope scope = translate_program(driver.global_defs);
scope.compile();
gen_llvm(scope);
} catch(unification_error& err) {

43
13/parse_driver.hpp Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include <string>
#include <fstream>
#include "definition.hpp"
#include "location.hh"
#include "parser.hpp"
struct parse_driver;
void scanner_init(parse_driver* d, yyscan_t* scanner);
void scanner_destroy(yyscan_t* scanner);
struct parse_driver {
std::string file_name;
std::ifstream file_stream;
yy::location location;
size_t file_offset;
std::vector<size_t> line_offsets;
definition_group global_defs;
parse_driver(const std::string& file)
: file_name(file) {}
void run_parse() {
file_stream.open(file_name);
if(!file_stream.good()) throw 0;
yyscan_t scanner;
scanner_init(this, &scanner);
yy::parser parser(scanner, *this);
parser();
scanner_destroy(&scanner);
}
int get() {
return file_stream.get();
}
};
#define YY_DECL yy::parser::symbol_type yylex(yyscan_t yyscanner, parse_driver& drv)
YY_DECL;

View File

@ -1,17 +1,21 @@
%{
%code requires {
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include "ast.hpp"
#include "definition.hpp"
#include "parser.hpp"
#include "parsed_type.hpp"
definition_group global_defs;
class parse_driver;
using yyscan_t = void*;
}
extern yy::parser::symbol_type yylex();
%param { yyscan_t scanner }
%param { parse_driver& drv }
%}
%code {
#include "parse_driver.hpp"
}
%token BACKSLASH
%token PLUS
@ -59,7 +63,7 @@ extern yy::parser::symbol_type yylex();
%%
program
: definitions { global_defs = std::move($1); global_defs.vis = visibility::global; }
: definitions { $1.vis = visibility::global; std::swap(drv.global_defs, $1); }
;
definitions

View File

@ -1,16 +1,22 @@
%option noyywrap
%option reentrant
%{
#include <iostream>
#include "ast.hpp"
#include "definition.hpp"
#include "parse_driver.hpp"
#include "parser.hpp"
yy::parser::location_type location;
#define YY_DECL yy::parser::symbol_type yylex()
#define YY_EXTRA_TYPE parse_driver*
#define YY_USER_ACTION location.step(); location.columns(yyleng);
#define YY_INPUT(buf,result,max_size) \
{ \
int c = yyextra->get(); \
result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
}
%}
%%
@ -41,3 +47,10 @@ in { return yy::parser::make_IN(location); }
<<EOF>> { return yy::parser::make_YYEOF(location); }
%%
void scanner_init(parse_driver* d, yyscan_t* scanner) {
yylex_init_extra(d, scanner);
}
void scanner_destroy(yyscan_t* scanner) {
yylex_destroy(*scanner);
}