2021-04-06 19:13:24 +01:00
|
|
|
/* -*- C++ -*-
|
2021-10-12 20:51:03 +01:00
|
|
|
* Serene Programming Language
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 2.
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-07-16 15:48:06 +01:00
|
|
|
*/
|
|
|
|
|
2021-04-06 18:28:50 +01:00
|
|
|
#include "serene/reader/reader.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-09-12 14:01:02 +01:00
|
|
|
#include "serene/errors/constants.h"
|
2021-10-24 12:34:20 +01:00
|
|
|
#include "serene/exprs/expression.h"
|
2021-04-11 00:02:56 +01:00
|
|
|
#include "serene/exprs/list.h"
|
|
|
|
#include "serene/exprs/number.h"
|
|
|
|
#include "serene/exprs/symbol.h"
|
2021-05-06 19:17:57 +01:00
|
|
|
#include "serene/namespace.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-09-28 17:11:50 +01:00
|
|
|
#include <llvm/ADT/StringRef.h>
|
2021-09-05 15:57:28 +01:00
|
|
|
#include <llvm/Support/Error.h>
|
2021-09-28 17:11:50 +01:00
|
|
|
#include <llvm/Support/ErrorHandling.h>
|
2021-09-05 15:57:28 +01:00
|
|
|
#include <llvm/Support/ErrorOr.h>
|
|
|
|
#include <llvm/Support/FormatVariadic.h>
|
|
|
|
#include <llvm/Support/MemoryBuffer.h>
|
|
|
|
#include <llvm/Support/SMLoc.h>
|
2021-09-28 17:11:50 +01:00
|
|
|
#include <mlir/IR/Diagnostics.h>
|
|
|
|
#include <mlir/IR/Location.h>
|
|
|
|
#include <mlir/Support/LogicalResult.h>
|
2021-10-17 14:57:17 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <cctype>
|
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
2020-07-23 21:47:13 +01:00
|
|
|
#include <string>
|
2020-07-16 15:48:06 +01:00
|
|
|
|
|
|
|
namespace serene {
|
2021-03-28 23:14:32 +01:00
|
|
|
|
|
|
|
namespace reader {
|
2021-11-09 22:30:47 +00:00
|
|
|
// LocationRange::LocationRange(const LocationRange &loc) {
|
|
|
|
// start = loc.start.clone();
|
|
|
|
// end = loc.end.clone();
|
|
|
|
// }
|
|
|
|
|
|
|
|
/// Return the string represenation of the location.
|
|
|
|
std::string Location::toString() const {
|
|
|
|
return llvm::formatv("{0}:{1}", line, col);
|
|
|
|
};
|
|
|
|
|
|
|
|
Location Location::clone() const {
|
|
|
|
return Location{ns, filename, c, line, col, knownLocation};
|
|
|
|
}
|
|
|
|
|
|
|
|
mlir::Location Location::toMLIRLocation(SereneContext &ctx) {
|
|
|
|
// TODO: Create a new Location attribute that is namespace base
|
|
|
|
if (filename.hasValue()) {
|
|
|
|
return mlir::FileLineColLoc::get(&ctx.mlirContext, filename.getValue(),
|
|
|
|
line, col);
|
|
|
|
}
|
|
|
|
return mlir::FileLineColLoc::get(&ctx.mlirContext, ns, line, col);
|
|
|
|
}
|
|
|
|
/// Increase the given location by one and set the line/col value in respect to
|
|
|
|
/// the `newline` in place.
|
|
|
|
/// \param loc The `Location` data
|
|
|
|
/// \param c A pointer to the current char that the location has to point to
|
|
|
|
void incLocation(Location &loc, const char *c) {
|
|
|
|
// TODO: Handle the end of line with respect to the OS.
|
|
|
|
// increase the current position in the buffer with respect to the end
|
|
|
|
// of line.
|
|
|
|
auto newline = *c == '\n';
|
|
|
|
|
|
|
|
if (!newline) {
|
|
|
|
loc.col++;
|
|
|
|
} else {
|
|
|
|
loc.line++;
|
|
|
|
loc.col = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// decrease the given location by one and set the line/col value in respect to
|
|
|
|
/// the `newline` in place.
|
|
|
|
/// \param loc The `Location` data
|
|
|
|
/// \param c A pointer to the current char that the location has to point to
|
|
|
|
void decLocation(Location &loc, const char *c) {
|
|
|
|
// TODO: Handle the end of line with respect to the OS.
|
|
|
|
// increase the current position in the buffer with respect to the end
|
|
|
|
// of line.
|
|
|
|
auto newline = *c == '\n';
|
|
|
|
|
|
|
|
if (newline) {
|
|
|
|
loc.line = loc.line == 0 ? 0 : loc.line - 1;
|
|
|
|
|
|
|
|
// We don't move back the `col` value because we simply don't know it
|
|
|
|
} else {
|
|
|
|
loc.col = loc.col == 0 ? 0 : loc.col - 1;
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 19:53:10 +00:00
|
|
|
|
2021-09-07 21:47:29 +01:00
|
|
|
Reader::Reader(SereneContext &ctx, llvm::StringRef buffer, llvm::StringRef ns,
|
|
|
|
llvm::Optional<llvm::StringRef> filename)
|
2021-09-12 14:01:02 +01:00
|
|
|
: ctx(ctx), ns(ns), filename(filename), buf(buffer),
|
2021-09-12 16:55:31 +01:00
|
|
|
currentLocation(Location(ns, filename)) {
|
|
|
|
READER_LOG("Setting the first char of the buffer");
|
2021-09-12 17:03:08 +01:00
|
|
|
currentChar = buf.begin() - 1;
|
2021-09-12 16:55:31 +01:00
|
|
|
currentPos = 1;
|
|
|
|
currentLocation.line = 1;
|
|
|
|
currentLocation.col = 1;
|
|
|
|
};
|
2021-04-11 00:02:56 +01:00
|
|
|
|
2021-09-07 21:47:29 +01:00
|
|
|
Reader::Reader(SereneContext &ctx, llvm::MemoryBufferRef buffer,
|
|
|
|
llvm::StringRef ns, llvm::Optional<llvm::StringRef> filename)
|
2021-09-12 16:55:31 +01:00
|
|
|
: Reader(ctx, buffer.getBuffer(), ns, filename){};
|
2020-07-23 21:47:13 +01:00
|
|
|
|
|
|
|
Reader::~Reader() { READER_LOG("Destroying the reader"); }
|
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
void Reader::advanceByOne() {
|
|
|
|
currentChar++;
|
|
|
|
currentPos++;
|
|
|
|
currentLocation.col++;
|
2021-09-12 14:01:02 +01:00
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
if (*currentChar == '\n') {
|
|
|
|
READER_LOG("Detected end of line");
|
2021-03-28 23:14:32 +01:00
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
if (readEOL) {
|
|
|
|
currentLocation.col = 1;
|
|
|
|
currentLocation.line++;
|
2021-09-12 14:01:02 +01:00
|
|
|
}
|
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
readEOL = true;
|
|
|
|
} else {
|
|
|
|
if (readEOL) {
|
|
|
|
currentLocation.line++;
|
|
|
|
currentLocation.col = 1;
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2021-09-12 16:55:31 +01:00
|
|
|
readEOL = false;
|
2020-07-21 22:23:11 +01:00
|
|
|
}
|
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
READER_LOG("Moving to Char: " << *currentChar << " at location: "
|
|
|
|
<< currentLocation.toString());
|
|
|
|
};
|
|
|
|
void Reader::advance(bool skipWhitespace) {
|
|
|
|
if (skipWhitespace) {
|
|
|
|
for (;;) {
|
2021-10-17 14:57:17 +01:00
|
|
|
const auto *next = currentChar + 1;
|
2021-09-12 14:01:02 +01:00
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
if (isspace(*next) == 0) {
|
2021-09-12 16:55:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-09-12 14:01:02 +01:00
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
advanceByOne();
|
|
|
|
}
|
2021-09-12 14:01:02 +01:00
|
|
|
} else {
|
2021-09-12 16:55:31 +01:00
|
|
|
advanceByOne();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *Reader::nextChar(bool skipWhitespace, unsigned count) {
|
|
|
|
if (!skipWhitespace) {
|
|
|
|
READER_LOG("Next char: " << *(currentChar + count));
|
|
|
|
return currentChar + count;
|
2021-09-12 14:01:02 +01:00
|
|
|
}
|
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
const auto *c = currentChar + 1;
|
|
|
|
while (isspace(*c) != 0) {
|
2021-09-12 16:55:31 +01:00
|
|
|
c++;
|
|
|
|
};
|
|
|
|
|
|
|
|
READER_LOG("Next char: " << *c);
|
|
|
|
return c;
|
2021-03-29 01:02:29 +01:00
|
|
|
};
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-09-05 15:57:28 +01:00
|
|
|
bool Reader::isEndOfBuffer(const char *c) {
|
2021-09-12 14:01:02 +01:00
|
|
|
return *c == '\0' || currentPos > buf.size() || *c == EOF;
|
2021-09-05 15:57:28 +01:00
|
|
|
};
|
|
|
|
|
2021-09-12 14:01:02 +01:00
|
|
|
Location Reader::getCurrentLocation() { return currentLocation.clone(); };
|
2021-09-05 15:57:28 +01:00
|
|
|
|
2021-04-11 00:02:56 +01:00
|
|
|
/// A predicate function indicating whether the given char `c` is a valid
|
|
|
|
/// char for the starting point of a symbol or not.
|
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
|
|
|
}
|
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
return std::isalnum(c) != 0;
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-04-11 15:13:10 +01:00
|
|
|
/// Reads a number,
|
|
|
|
/// \param neg whether to read a negative number or not.
|
2021-10-24 12:34:20 +01:00
|
|
|
exprs::MaybeNode Reader::readNumber(bool neg) {
|
2021-07-17 19:39:56 +01:00
|
|
|
READER_LOG("Reading a number...");
|
2021-03-31 19:37:32 +01:00
|
|
|
std::string number(neg ? "-" : "");
|
|
|
|
bool floatNum = false;
|
2021-06-13 22:58:46 +01:00
|
|
|
bool empty = false;
|
2021-03-31 19:37:32 +01:00
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
const auto *c = nextChar();
|
2021-09-12 16:55:31 +01:00
|
|
|
advance();
|
2021-03-31 19:37:32 +01:00
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
LocationRange loc(getCurrentLocation());
|
2021-03-31 19:37:32 +01:00
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
if (isdigit(*c) == 0) {
|
2021-10-24 12:34:20 +01:00
|
|
|
return exprs::makeErrorNode(loc, errors::InvalidDigitForNumber);
|
2021-09-12 16:55:31 +01:00
|
|
|
}
|
2021-09-05 15:57:28 +01:00
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
for (;;) {
|
2021-09-05 15:57:28 +01:00
|
|
|
number += *c;
|
2021-09-12 16:55:31 +01:00
|
|
|
c = nextChar(false);
|
2021-03-31 19:37:32 +01:00
|
|
|
empty = false;
|
2021-09-12 16:55:31 +01:00
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
if ((isdigit(*c) != 0) || *c == '.') {
|
|
|
|
if (*c == '.' && floatNum) {
|
2021-09-12 16:55:31 +01:00
|
|
|
loc = LocationRange(getCurrentLocation());
|
|
|
|
ctx.diagEngine->emitSyntaxError(loc, errors::TwoFloatPoints);
|
2021-10-17 16:06:52 +01:00
|
|
|
terminate(ctx, 1);
|
2021-09-12 16:55:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*c == '.') {
|
|
|
|
floatNum = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
advance();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2021-03-31 19:37:32 +01:00
|
|
|
}
|
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
if (((std::isalpha(*c) != 0) && !empty) || empty) {
|
2021-09-12 19:50:18 +01:00
|
|
|
advance();
|
|
|
|
loc.start = getCurrentLocation();
|
2021-09-12 14:01:02 +01:00
|
|
|
ctx.diagEngine->emitSyntaxError(loc, errors::InvalidDigitForNumber);
|
2021-10-17 16:06:52 +01:00
|
|
|
terminate(ctx, 1);
|
2021-09-07 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
loc.end = getCurrentLocation();
|
|
|
|
return exprs::make<exprs::Number>(loc, number, neg, floatNum);
|
2021-03-31 19:37:32 +01:00
|
|
|
};
|
|
|
|
|
2021-04-11 15:13:10 +01:00
|
|
|
/// Reads a symbol. If the symbol looks like a number
|
|
|
|
/// If reads it as number
|
2021-10-24 12:34:20 +01:00
|
|
|
exprs::MaybeNode Reader::readSymbol() {
|
2021-09-05 15:57:28 +01:00
|
|
|
READER_LOG("Reading a symbol...");
|
2021-09-12 14:01:02 +01:00
|
|
|
LocationRange loc;
|
2021-10-17 14:57:17 +01:00
|
|
|
const auto *c = nextChar();
|
2020-07-21 22:23:11 +01:00
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
if (!this->isValidForIdentifier(*c) || isEndOfBuffer(c) ||
|
|
|
|
(isspace(*c) != 0)) {
|
2021-09-12 16:55:31 +01:00
|
|
|
advance();
|
|
|
|
loc = LocationRange(getCurrentLocation());
|
2021-09-12 17:49:51 +01:00
|
|
|
std::string msg;
|
|
|
|
|
|
|
|
if (*c == ')') {
|
|
|
|
msg = "An extra ')' is detected.";
|
|
|
|
}
|
|
|
|
|
2021-10-24 12:34:20 +01:00
|
|
|
return exprs::makeErrorNode(loc, errors::InvalidCharacterForSymbol, msg);
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-09-05 15:57:28 +01:00
|
|
|
if (*c == '-') {
|
2021-10-17 14:57:17 +01:00
|
|
|
const auto *next = nextChar(false, 2);
|
|
|
|
if (isdigit(*next) != 0) {
|
2021-09-12 16:55:31 +01:00
|
|
|
// Swallow the -
|
|
|
|
advance();
|
2021-03-31 19:37:32 +01:00
|
|
|
return readNumber(true);
|
|
|
|
}
|
|
|
|
}
|
2021-04-11 15:13:10 +01:00
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
if (isdigit(*c) != 0) {
|
2021-03-31 19:37:32 +01:00
|
|
|
return readNumber(false);
|
|
|
|
}
|
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
std::string sym;
|
2021-09-12 16:55:31 +01:00
|
|
|
advance();
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
for (;;) {
|
2021-09-05 15:57:28 +01:00
|
|
|
sym += *c;
|
2021-09-12 16:55:31 +01:00
|
|
|
c = nextChar();
|
2020-07-22 18:58:15 +01:00
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
if (!isEndOfBuffer(c) &&
|
2021-10-17 14:57:17 +01:00
|
|
|
((((isspace(*c)) == 0) && this->isValidForIdentifier(*c)))) {
|
2021-09-12 16:55:31 +01:00
|
|
|
advance();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2020-07-23 21:47:13 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2022-01-01 22:15:30 +00:00
|
|
|
// TODO: Make sure that the symbol has 0 or 1 '/'.
|
|
|
|
|
|
|
|
// TODO: Make sure that `/` is not at the start or at the end of the symbol
|
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
loc.end = getCurrentLocation();
|
2022-01-01 22:15:30 +00:00
|
|
|
return exprs::makeSuccessfulNode<exprs::Symbol>(loc, sym, this->ns);
|
2020-07-23 21:47:13 +01:00
|
|
|
};
|
2021-03-31 19:37:32 +01:00
|
|
|
|
2021-04-11 15:13:10 +01:00
|
|
|
/// Reads a list recursively
|
2021-10-24 12:34:20 +01:00
|
|
|
exprs::MaybeNode Reader::readList() {
|
2021-07-17 19:39:56 +01:00
|
|
|
READER_LOG("Reading a list...");
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-10-17 14:57:17 +01:00
|
|
|
const auto *c = nextChar();
|
2021-09-12 16:55:31 +01:00
|
|
|
advance();
|
|
|
|
|
|
|
|
auto list = exprs::makeAndCast<exprs::List>(getCurrentLocation());
|
2021-07-19 15:44:39 +01:00
|
|
|
|
|
|
|
// TODO: Replace the assert with an actual check.
|
2021-09-05 15:57:28 +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-10-17 14:57:17 +01:00
|
|
|
const auto *c = nextChar(true);
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-09-05 15:57:28 +01:00
|
|
|
if (isEndOfBuffer(c)) {
|
2021-09-12 16:55:31 +01:00
|
|
|
advance(true);
|
|
|
|
advance();
|
2021-09-12 14:01:02 +01:00
|
|
|
list->location.end = getCurrentLocation();
|
2021-10-24 12:34:20 +01:00
|
|
|
return exprs::makeErrorNode(list->location, errors::EOFWhileScaningAList);
|
2021-09-05 15:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (*c) {
|
2020-07-23 21:47:13 +01:00
|
|
|
case ')':
|
2021-09-12 16:55:31 +01:00
|
|
|
advance(true);
|
|
|
|
advance();
|
2021-06-13 22:58:46 +01:00
|
|
|
list_terminated = true;
|
2021-09-05 15:57:28 +01:00
|
|
|
list->location.end = getCurrentLocation();
|
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-09-12 16:55:31 +01:00
|
|
|
advance(true);
|
2021-10-24 12:34:20 +01:00
|
|
|
auto expr = readExpr();
|
|
|
|
if (!expr) {
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->append(expr.getValue());
|
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-10-24 12:34:20 +01:00
|
|
|
return exprs::MaybeNode::success(list);
|
2021-04-11 00:02:56 +01:00
|
|
|
};
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-04-11 15:13:10 +01:00
|
|
|
/// Reads an expression by dispatching to the proper reader function.
|
2021-10-24 12:34:20 +01:00
|
|
|
exprs::MaybeNode Reader::readExpr() {
|
2021-10-17 14:57:17 +01:00
|
|
|
const auto *c = nextChar(true);
|
2021-09-12 16:55:31 +01:00
|
|
|
|
2021-09-12 14:01:02 +01:00
|
|
|
READER_LOG("Read char at `readExpr`: " << *c);
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-09-05 15:57:28 +01:00
|
|
|
if (isEndOfBuffer(c)) {
|
2021-10-24 12:34:20 +01:00
|
|
|
return exprs::EmptyNode;
|
2021-09-05 15:57:28 +01:00
|
|
|
}
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-09-05 15:57:28 +01:00
|
|
|
switch (*c) {
|
|
|
|
case '(': {
|
2021-09-12 16:55:31 +01:00
|
|
|
advance(true);
|
2021-03-31 19:37:32 +01:00
|
|
|
return readList();
|
2021-03-29 01:02:29 +01:00
|
|
|
}
|
2021-09-07 21:47:29 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
default:
|
2021-09-12 16:55:31 +01:00
|
|
|
advance(true);
|
2021-03-31 19:37:32 +01:00
|
|
|
return readSymbol();
|
2020-07-16 15:48:06 +01:00
|
|
|
}
|
2021-04-11 00:02:56 +01:00
|
|
|
};
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-04-11 15:13:10 +01:00
|
|
|
/// Reads all the expressions in the reader's buffer as an AST.
|
|
|
|
/// Each expression type (from the reader perspective) has a
|
|
|
|
/// reader function.
|
2021-10-24 12:34:20 +01:00
|
|
|
exprs::MaybeAst Reader::read() {
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-09-05 15:57:28 +01:00
|
|
|
for (size_t current_pos = 0; current_pos < buf.size();) {
|
2021-10-17 14:57:17 +01:00
|
|
|
const auto *c = nextChar(true);
|
2021-09-12 16:55:31 +01:00
|
|
|
|
2021-09-07 21:47:29 +01:00
|
|
|
if (isEndOfBuffer(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-12 16:55:31 +01:00
|
|
|
advance(true);
|
2021-09-07 21:47:29 +01:00
|
|
|
|
2021-10-24 12:34:20 +01:00
|
|
|
auto tmp = readExpr();
|
2021-04-11 15:13:10 +01:00
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
if (tmp) {
|
2021-10-24 12:34:20 +01:00
|
|
|
if (tmp.getValue() == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->ast.push_back(move(tmp.getValue()));
|
|
|
|
|
2021-09-05 15:57:28 +01:00
|
|
|
} else {
|
2021-10-24 12:34:20 +01:00
|
|
|
return exprs::MaybeAst::error(tmp.getError());
|
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-10-24 12:34:20 +01:00
|
|
|
return exprs::MaybeAst::success(std::move(this->ast));
|
2021-04-11 00:02:56 +01:00
|
|
|
};
|
2021-03-22 19:53:10 +00:00
|
|
|
|
2021-10-24 12:34:20 +01:00
|
|
|
exprs::MaybeAst read(SereneContext &ctx, const llvm::StringRef input,
|
|
|
|
llvm::StringRef ns,
|
|
|
|
llvm::Optional<llvm::StringRef> filename) {
|
2021-09-07 21:47:29 +01:00
|
|
|
reader::Reader r(ctx, input, ns, filename);
|
2021-09-05 15:57:28 +01:00
|
|
|
auto ast = r.read();
|
|
|
|
return ast;
|
2021-03-22 19:53:10 +00:00
|
|
|
}
|
2021-04-23 16:48:14 +01:00
|
|
|
|
2021-10-24 12:34:20 +01:00
|
|
|
exprs::MaybeAst read(SereneContext &ctx, const llvm::MemoryBufferRef input,
|
|
|
|
llvm::StringRef ns,
|
|
|
|
llvm::Optional<llvm::StringRef> filename) {
|
2021-09-07 21:47:29 +01:00
|
|
|
reader::Reader r(ctx, input, ns, filename);
|
|
|
|
|
2021-04-23 16:48:14 +01:00
|
|
|
auto ast = r.read();
|
|
|
|
return ast;
|
|
|
|
}
|
2021-03-28 23:14:32 +01:00
|
|
|
} // namespace reader
|
2020-07-23 21:47:13 +01:00
|
|
|
} // namespace serene
|