GSC Interpreter
A Turing-complete interpreter developed for a compiler course
Loading...
Searching...
No Matches
main.cpp File Reference
#include "gsc/error.hpp"
#include "gsc/interpreter.hpp"
#include "gsc/parser.hpp"
#include "gsc/scanner.hpp"
#include "gsc/token.hpp"
#include <fstream>
#include <iostream>
#include <vector>
Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

void runFile (std::string_view filename)
 
void runPrompt ()
 
int main (int argc, char *argv[])
 
std::string readFile (std::string_view filename)
 
void run (std::string_view program)
 

Variables

Interpreter interpreter {}
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 15 of file main.cpp.

15 {
16 if (argc > 2) {
17 std::cerr << "Usage: " << argv[0] << " [file.gsc]" << std::endl;
18 std::exit(EXIT_FAILURE);
19 } else if (argc == 2) {
20 runFile(argv[1]);
21 } else {
22 runPrompt();
23 }
24}
void runPrompt()
Definition main.cpp:67
void runFile(std::string_view filename)
Definition main.cpp:53
Here is the call graph for this function:

◆ readFile()

std::string readFile ( std::string_view  filename)

Definition at line 26 of file main.cpp.

26 {
27 std::ifstream file(filename.data());
28
29 if (!file) {
30 std::cerr << "Could not open file: " << filename << std::endl;
31 std::exit(EXIT_FAILURE);
32 } else {
33 std::string content{std::istreambuf_iterator<char>(file),
34 std::istreambuf_iterator<char>()};
35 return content;
36 }
37}

◆ run()

void run ( std::string_view  program)

Definition at line 39 of file main.cpp.

39 {
40 Scanner scanner{program};
41 scanner.scanTokens();
42 std::vector<Token> tokens = scanner.getTokens();
43 Parser parser{tokens};
44 std::vector<std::shared_ptr<Stmt>> statements = parser.parse();
45
46 if (hadError) {
47 std::cerr << "Error while parsing the program." << std::endl;
48 } else {
49 interpreter.interpret(statements);
50 }
51}
void interpret(const std::vector< std::shared_ptr< Stmt > > &statements)
Interpret the given statement list and execute them.
Parser module for the GSC programming language.
Definition parser.hpp:16
std::vector< std::shared_ptr< Stmt > > parse()
Parses the tokens and returns a vector of statements.
Definition parser.cpp:7
Lexical analyzer for the GSC programming language.
Definition scanner.hpp:23
void scanTokens()
Scans the entire source code and generates a list of tokens.
Definition scanner.cpp:171
bool hadError
Definition error.hpp:6
Interpreter interpreter
Definition main.cpp:13
Here is the caller graph for this function:

◆ runFile()

void runFile ( std::string_view  filename)

Definition at line 53 of file main.cpp.

53 {
54 std::string program = readFile(filename);
55 run(program);
56
57 if (hadError) {
58 std::cerr << "Error while running file: " << filename << std::endl;
59 std::exit(EXIT_FAILURE);
60 } else if (hadRuntimeError) {
61 std::cerr << "Runtime error occurred while running file: " << filename
62 << std::endl;
63 std::exit(EXIT_FAILURE);
64 }
65}
bool hadRuntimeError
Definition error.hpp:7
void run(std::string_view program)
Definition main.cpp:39
std::string readFile(std::string_view filename)
Definition main.cpp:26
Here is the call graph for this function:

◆ runPrompt()

void runPrompt ( )

Definition at line 67 of file main.cpp.

67 {
68 while (true) {
69 std::cout << ">> ";
70 std::string line;
71 if (!std::getline(std::cin, line)) {
72 std::cout << std::endl;
73 break;
74 } else {
75 run(line);
76 hadError = false; // Reset error state for the next line
77 }
78 }
79}
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ interpreter

Interpreter interpreter {}

Definition at line 13 of file main.cpp.

13{};