2020-07-16 15:48:06 +01:00
|
|
|
/**
|
|
|
|
* Serene programming language.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
2020-07-23 21:47:13 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-04-06 18:28:50 +01:00
|
|
|
#include "serene/reader/reader.h"
|
2021-03-22 19:53:10 +00:00
|
|
|
#include "serene/error.hpp"
|
2020-07-16 15:48:06 +01:00
|
|
|
#include "serene/list.hpp"
|
2021-03-31 19:37:32 +01:00
|
|
|
#include "serene/number.hpp"
|
2020-07-23 21:47:13 +01:00
|
|
|
#include "serene/symbol.hpp"
|
2021-03-23 22:31:20 +00:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2020-07-23 21:47:13 +01:00
|
|
|
#include <assert.h>
|
2021-03-22 19:53:10 +00:00
|
|
|
#include <fstream>
|
2020-07-23 21:47:13 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2020-07-16 15:48:06 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace serene {
|
2021-03-28 23:14:32 +01:00
|
|
|
|
|
|
|
namespace reader {
|
2021-03-22 19:53:10 +00:00
|
|
|
Reader::Reader(const string input) { this->setInput(input); };
|
|
|
|
|
2021-03-28 23:14:32 +01:00
|
|
|
/**
|
|
|
|
* Set the input of the reader.
|
|
|
|
* @param input Set the input to the given string
|
|
|
|
*/
|
2021-03-22 19:53:10 +00:00
|
|
|
void Reader::setInput(const string input) {
|
2020-07-23 21:47:13 +01:00
|
|
|
input_stream.write(input.c_str(), input.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
Reader::~Reader() { READER_LOG("Destroying the reader"); }
|
|
|
|
|
2021-03-28 23:14:32 +01:00
|
|
|
/**
|
|
|
|
* Return the next character in the buffer.
|
|
|
|
* @param skip_whitespace If true it will skip whitespaces and EOL chars
|
|
|
|
*/
|
2021-03-31 19:37:32 +01:00
|
|
|
char Reader::getChar(bool skip_whitespace) {
|
2020-07-23 21:47:13 +01:00
|
|
|
for (;;) {
|
|
|
|
char c = input_stream.get();
|
2021-03-29 01:02:29 +01:00
|
|
|
|
|
|
|
this->current_char = c;
|
2021-03-28 23:14:32 +01:00
|
|
|
inc_location(current_location, c == '\n');
|
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
if (skip_whitespace == true && isspace(c)) {
|
2021-03-28 23:14:32 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return c;
|
|
|
|
}
|
2020-07-21 22:23:11 +01:00
|
|
|
}
|
2020-07-23 21:47:13 +01:00
|
|
|
};
|
2020-07-21 22:23:11 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
void Reader::ungetChar() {
|
2021-03-29 01:02:29 +01:00
|
|
|
input_stream.unget();
|
|
|
|
// The char that we just unget
|
|
|
|
dec_location(current_location, this->current_char == '\n');
|
|
|
|
};
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
bool Reader::isValidForIdentifier(char c) {
|
2020-07-23 21:47:13 +01:00
|
|
|
switch (c) {
|
2021-03-31 19:37:32 +01:00
|
|
|
case '!':
|
|
|
|
case '$':
|
|
|
|
case '%':
|
|
|
|
case '&':
|
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '.':
|
|
|
|
case '~':
|
|
|
|
case '/':
|
|
|
|
case ':':
|
|
|
|
case '<':
|
|
|
|
case '=':
|
|
|
|
case '>':
|
|
|
|
case '?':
|
|
|
|
case '@':
|
|
|
|
case '^':
|
|
|
|
case '_':
|
2020-07-23 21:47:13 +01:00
|
|
|
return true;
|
2020-07-16 15:48:06 +01:00
|
|
|
}
|
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= '0' && c <= '9')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
ast_node Reader::readNumber(bool neg) {
|
|
|
|
std::string number(neg ? "-" : "");
|
|
|
|
bool floatNum = false;
|
|
|
|
bool empty = false;
|
|
|
|
|
|
|
|
LocationRange loc;
|
|
|
|
char c = getChar(false);
|
|
|
|
|
|
|
|
loc.start = current_location;
|
|
|
|
|
|
|
|
while (c != EOF &&
|
|
|
|
((!(isspace(c)) && ((c >= '0' && c <= '9') | (c == '.'))))) {
|
|
|
|
|
|
|
|
if (c == '.' && floatNum == true) {
|
|
|
|
|
|
|
|
llvm::errs() << "Two float points in a number?\n";
|
|
|
|
// TODO: Return a proper error
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '.') {
|
|
|
|
floatNum = true;
|
|
|
|
}
|
|
|
|
number += c;
|
|
|
|
c = getChar(false);
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty) {
|
|
|
|
ungetChar();
|
|
|
|
loc.end = current_location;
|
|
|
|
return makeNumber(loc, number, neg, floatNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
ast_node Reader::readSymbol() {
|
2020-07-23 21:47:13 +01:00
|
|
|
bool empty = true;
|
2021-03-31 19:37:32 +01:00
|
|
|
char c = getChar(false);
|
2020-07-21 22:23:11 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
READER_LOG("Reading symbol");
|
2021-03-31 19:37:32 +01:00
|
|
|
if (!this->isValidForIdentifier(c)) {
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
// TODO: Replece this with a tranceback function or something to raise
|
|
|
|
// synatx error.
|
2021-03-31 19:37:32 +01:00
|
|
|
// fmt::print("Invalid character at the start of a symbol: '{}'\n", c);
|
|
|
|
llvm::errs() << fmt::format(
|
|
|
|
"Invalid character at the start of a symbol: '{}'\n", c);
|
2020-07-23 21:47:13 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
if (c == '-') {
|
|
|
|
char next = getChar(false);
|
|
|
|
if (next >= '0' && next <= '9') {
|
|
|
|
ungetChar();
|
|
|
|
return readNumber(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
ungetChar();
|
|
|
|
return readNumber(false);
|
|
|
|
}
|
|
|
|
|
2021-03-29 01:02:29 +01:00
|
|
|
std::string sym("");
|
|
|
|
LocationRange loc;
|
|
|
|
loc.start = current_location;
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
while (c != EOF && ((!(isspace(c)) && this->isValidForIdentifier(c)))) {
|
2020-07-23 21:47:13 +01:00
|
|
|
sym += c;
|
2021-03-31 19:37:32 +01:00
|
|
|
c = getChar(false);
|
2020-07-23 21:47:13 +01:00
|
|
|
empty = false;
|
|
|
|
}
|
2020-07-22 18:58:15 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
if (!empty) {
|
2021-03-31 19:37:32 +01:00
|
|
|
ungetChar();
|
2021-03-29 01:02:29 +01:00
|
|
|
loc.end = current_location;
|
|
|
|
return makeSymbol(loc, sym);
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
// TODO: it should never happens
|
|
|
|
return nullptr;
|
|
|
|
};
|
2021-03-31 19:37:32 +01:00
|
|
|
|
2021-03-29 01:02:29 +01:00
|
|
|
// std::unique_ptr<List> list
|
2021-03-31 19:37:32 +01:00
|
|
|
ast_list_node Reader::readList() {
|
2021-03-29 01:02:29 +01:00
|
|
|
auto list = makeList(current_location);
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
char c = getChar(true);
|
2020-07-23 21:47:13 +01:00
|
|
|
assert(c == '(');
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
bool list_terminated = false;
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
do {
|
2021-03-31 19:37:32 +01:00
|
|
|
char c = getChar(true);
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
switch (c) {
|
|
|
|
case EOF:
|
2021-03-25 00:53:44 +00:00
|
|
|
throw ReadError(const_cast<char *>("EOF reached before closing of list"));
|
2020-07-23 21:47:13 +01:00
|
|
|
case ')':
|
|
|
|
list_terminated = true;
|
2021-03-29 01:02:29 +01:00
|
|
|
list->location->end = current_location;
|
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
break;
|
2020-07-21 22:23:11 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
default:
|
2021-03-31 19:37:32 +01:00
|
|
|
ungetChar();
|
|
|
|
list->append(readExpr());
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
} while (!list_terminated);
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-29 01:02:29 +01:00
|
|
|
return list;
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
ast_node Reader::readExpr() {
|
|
|
|
char c = getChar(false);
|
2020-07-23 21:47:13 +01:00
|
|
|
READER_LOG("CHAR: {}", c);
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
ungetChar();
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
switch (c) {
|
2021-03-29 01:02:29 +01:00
|
|
|
case '(': {
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-31 19:37:32 +01:00
|
|
|
return readList();
|
2021-03-29 01:02:29 +01:00
|
|
|
}
|
2020-07-23 21:47:13 +01:00
|
|
|
case EOF:
|
|
|
|
return nullptr;
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
default:
|
2021-03-31 19:37:32 +01:00
|
|
|
return readSymbol();
|
2020-07-16 15:48:06 +01:00
|
|
|
}
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-23 22:31:20 +00:00
|
|
|
std::unique_ptr<ast_tree> Reader::read() {
|
2021-03-31 19:37:32 +01:00
|
|
|
char c = getChar(true);
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
while (c != EOF) {
|
2021-03-31 19:37:32 +01:00
|
|
|
ungetChar();
|
|
|
|
auto tmp{readExpr()};
|
2020-07-23 21:47:13 +01:00
|
|
|
if (tmp) {
|
|
|
|
this->ast.push_back(move(tmp));
|
2020-07-16 15:48:06 +01:00
|
|
|
}
|
2021-03-31 19:37:32 +01:00
|
|
|
c = getChar(true);
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-03-23 22:31:20 +00:00
|
|
|
return std::make_unique<ast_tree>(this->ast);
|
2021-03-22 19:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::dumpAST() {
|
2021-03-23 22:31:20 +00:00
|
|
|
ast_tree ast = *this->read();
|
2021-03-22 19:53:10 +00:00
|
|
|
std::string result = "";
|
|
|
|
for (auto &node : ast) {
|
|
|
|
result = fmt::format("{0} {1}", result, node->dumpAST());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 22:31:20 +00:00
|
|
|
std::unique_ptr<ast_tree> FileReader::read() {
|
2021-03-25 00:53:44 +00:00
|
|
|
|
|
|
|
// TODO: Add support for relative path as well
|
2021-03-23 22:31:20 +00:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
|
|
|
|
llvm::MemoryBuffer::getFileOrSTDIN(file);
|
2021-03-22 19:53:10 +00:00
|
|
|
|
2021-03-23 22:31:20 +00:00
|
|
|
if (std::error_code EC = fileOrErr.getError()) {
|
|
|
|
llvm::errs() << "Could not open input file: " << EC.message() << "\n";
|
2021-03-25 00:53:44 +00:00
|
|
|
llvm::errs() << fmt::format("File: '{}'\n", file);
|
|
|
|
llvm::errs() << "Use absolute path for now\n";
|
2021-03-23 22:31:20 +00:00
|
|
|
return nullptr;
|
2021-03-22 19:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 22:31:20 +00:00
|
|
|
reader->setInput(fileOrErr.get()->getBuffer().str());
|
|
|
|
return reader->read();
|
2021-03-22 19:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileReader::dumpAST() {
|
2021-03-23 22:31:20 +00:00
|
|
|
auto maybeAst = this->read();
|
|
|
|
ast_tree ast;
|
|
|
|
|
|
|
|
if (maybeAst) {
|
|
|
|
ast = *maybeAst;
|
|
|
|
}
|
|
|
|
|
2021-03-22 19:53:10 +00:00
|
|
|
std::string result = "";
|
|
|
|
for (auto &node : ast) {
|
|
|
|
result = fmt::format("{0} {1}", result, node->dumpAST());
|
|
|
|
}
|
|
|
|
cout << result << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReader::~FileReader() {
|
|
|
|
delete this->reader;
|
|
|
|
READER_LOG("Destroying the file reader");
|
|
|
|
}
|
2021-03-28 23:14:32 +01:00
|
|
|
} // namespace reader
|
2020-07-23 21:47:13 +01:00
|
|
|
} // namespace serene
|