Skip to content

Commit

Permalink
AStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcussacapuces91 committed Sep 2, 2024
1 parent 7134bcf commit 2884e27
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 123 deletions.
33 changes: 21 additions & 12 deletions include/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ class Command : private std::vector<Token*> {
public:
Command(const std::vector<Token*>& aTokens) : std::vector<Token*>(aTokens) {}

friend std::ostream& operator<<(std::ostream&, const Command&);
friend std::ostream& operator<<(std::ostream&, const Command&);
};

std::ostream& operator<<(std::ostream& out, const Command& aCommand) {
std::ostream& operator<<(std::ostream& out, const Command& aCommand)
{
for (auto&& token : aCommand) {
out << (token == *aCommand.begin() ? "" : " ") << *token;
}
return out;
}


std::ostream& operator<<(std::ostream& out, const std::vector<Command>& aCommands) {
std::ostream& operator<<(std::ostream& out, const std::vector<Command>& aCommands)
{
for (auto&& command : aCommands) {
out << (&command == &(*aCommands.begin()) ? "" : " : ") << command;
}
Expand All @@ -62,10 +64,12 @@ class Interpreter {
Interpreter(std::istream& aIn = std::cin, std::ostream& aOut = std::cout, std::ostream& aErr = std::cerr) :
in(aIn),
out(aOut),
err(aErr) {
err(aErr)
{
}

error_t load(std::ifstream& aFile) {
error_t load(std::ifstream& aFile)
{
std::string line;
while (std::getline(aFile, line)) {
// Empty line?
Expand All @@ -89,12 +93,14 @@ class Interpreter {
if (pTC->getType() == Token::INTEGER) {
lineNumber = std::stoul(pTC->getValue());
++token;
} else {
}
else {
err << "Syntax Error: A line number must be an INTEGER!" << std::endl;
err << line << std::endl;
return SYNTAX_ERROR;
}
} else {
}
else {
err << "Syntax Error: A line number must be an INTEGER!" << std::endl;
err << line << std::endl;
return SYNTAX_ERROR;
Expand All @@ -108,8 +114,8 @@ class Interpreter {
}
out << std::setw(5) << lineNumber << " " << commands << std::endl;



}
return OK;
}
Expand All @@ -120,7 +126,8 @@ class Interpreter {
* @param stop Iterator after the last token.
* @return A vector of tokens.
**/
Command commandSlicer(std::vector<Token*>::const_iterator& start, const std::vector<Token*>::const_iterator& stop) {
Command commandSlicer(std::vector<Token*>::const_iterator& start, const std::vector<Token*>::const_iterator& stop)
{
std::vector<Token*> res;
for (auto token = start; token != stop; ++token) {
if (const auto pTSep = dynamic_cast<TokenSeparator*>(*token)) {
Expand All @@ -136,7 +143,8 @@ class Interpreter {
}


std::string toString() const {
std::string toString() const
{
std::ostringstream s;
s << "MS-Basic - Copyright (C) 2024 by M. SIBERT" << std::endl;
s << "Ready." << std::endl;
Expand All @@ -155,7 +163,8 @@ class Interpreter {
std::map<unsigned, std::vector<Command> > program;
};

std::ostream& operator<<(std::ostream& out, const Interpreter& aInterpreter) {
std::ostream& operator<<(std::ostream& out, const Interpreter& aInterpreter)
{
out << aInterpreter.toString() << std::endl;
return out;
}
17 changes: 8 additions & 9 deletions include/tokenizer.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright [2024] Marc SIBERT
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -24,12 +24,11 @@

class Token;

class Tokenizer
{
public:
std::vector<Token*> tokenize(const std::string& aLine, bool& err, int& pos) const;
class Tokenizer {
public:
std::vector<Token*> tokenize(const std::string& aLine, bool& err, int& pos) const;

protected:
protected:

private:
private:
};
46 changes: 23 additions & 23 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/**
* Copyright [2024] Marc SIBERT
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

#include <iostream>
#include <fstream>
#include <string>
Expand All @@ -22,33 +22,33 @@

int main()
{
Interpreter interpreter;
std::cout << interpreter;
Interpreter interpreter;
std::cout << interpreter;

/*
while (std::cin) std::cin >> interpreter;
*/
/*
while (std::cin) std::cin >> interpreter;
*/

Tokenizer tokenizer;

std::ifstream file;
file.open("C:\\Users\\marc\\Documents\\GitHub\\MS-Basic\\eliza.bas", std::ios::in);
if (!file) {
std::cerr << "Error opening file!" << std::endl;
exit(-1);
std::ifstream file;
file.open("C:\\Users\\marc\\Documents\\GitHub\\MS-Basic\\eliza.bas", std::ios::in);

if (!file) {
std::cerr << "Error opening file!" << std::endl;
exit(-1);
}

interpreter.load(file);

/*
std::string line;
while (std::getline(file, line)) {
interpreter.interpret(line);
}
*/
/*
std::string line;
while (std::getline(file, line)) {
interpreter.interpret(line);
}
*/

file.close();
file.close();

return 0;
return 0;
}
110 changes: 55 additions & 55 deletions src/tokenizer.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/**
* Copyright [2024] Marc SIBERT
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

#include <vector>
#include <string>
#include <regex>
Expand All @@ -23,65 +23,65 @@


std::vector<Token*> Tokenizer::tokenize(const std::string& aLine, bool& err, int& pos) const
{
static const std::regex space("^\\s.*$");
{
static const std::regex space("^\\s.*$");

err = false;

std::vector<Token*> list;
auto posit = aLine.begin();
const auto end = aLine.end();

while (posit != end) {

if (std::regex_match(posit, end, space)) {
posit++;
continue;
}

auto pTCom = TokenComment::create(posit, end);
if (pTCom) {
list.push_back(pTCom);
continue;
}

auto pTIn = TokenInstruction::create(posit, end);
if (pTIn) {
list.push_back(pTIn);
continue;
}

auto pTId = TokenIdentifier::create(posit, end);
if (pTId) {
list.push_back(pTId);
continue;
}

auto pTOp = TokenOperator::create(posit, end);
if (pTOp) {
list.push_back(pTOp);
continue;
}

auto pTCo = TokenConstant::create(posit, end);
if (pTCo) {
list.push_back(pTCo);
continue;
}

auto pTSe = TokenSeparator::create(posit, end);
if (pTSe) {
list.push_back(pTSe);
continue;
}
std::vector<Token*> list;
auto posit = aLine.begin();
const auto end = aLine.end();

while (posit != end) {

if (std::regex_match(posit, end, space)) {
posit++;
continue;
}

auto pTCom = TokenComment::create(posit, end);
if (pTCom) {
list.push_back(pTCom);
continue;
}

auto pTIn = TokenInstruction::create(posit, end);
if (pTIn) {
list.push_back(pTIn);
continue;
}

auto pTId = TokenIdentifier::create(posit, end);
if (pTId) {
list.push_back(pTId);
continue;
}

auto pTOp = TokenOperator::create(posit, end);
if (pTOp) {
list.push_back(pTOp);
continue;
}

auto pTCo = TokenConstant::create(posit, end);
if (pTCo) {
list.push_back(pTCo);
continue;
}

auto pTSe = TokenSeparator::create(posit, end);
if (pTSe) {
list.push_back(pTSe);
continue;
}

err = true;
pos = 0;
for (auto i = aLine.begin(); i != posit; ++i) ++pos;
for (auto i = aLine.begin(); i != posit; ++i) ++pos;
for (auto it = list.begin(); it != list.end(); ++it) delete(*it);
list.clear();
return list;
}
}

return list;
return list;
}
Loading

0 comments on commit 2884e27

Please sign in to comment.