Skip to content

Commit

Permalink
update: add tests and multiple types
Browse files Browse the repository at this point in the history
Signed-off-by: Rohith-Raju <rohithraju488@gmail.com>
  • Loading branch information
Rohith-Raju committed Mar 12, 2024
1 parent 2b28d9b commit d65ca2d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions include/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class Interpreter {
} else if (checkCompatibility(op, left, right)) {
if (left.type == string_type && right.type == num_type)
return left.string_literal + std::to_string(right.num_literal);
else
return right.string_literal + std::to_string(left.num_literal);
else if (left.type == num_type && right.type == string_type)
return std::to_string(left.num_literal) + right.string_literal;
}
throw new RuntimeError(*op, "Error: Cannot evaluate expression");
case GREATER:
Expand Down
2 changes: 2 additions & 0 deletions include/Scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Token.h"
#include <Error.h>
#include <map>
#include <string>
#include <vector>

namespace tkn {
Expand All @@ -33,6 +34,7 @@ class Scanner {
std::vector<Token> scanTokens();
char peek();
void string();
std::string subString(int &start, int &end, std::string &str);
void number();
char advance();
void scanToken();
Expand Down
6 changes: 5 additions & 1 deletion src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ Expr *Parser::primary() {
if (match(NIL))
return new Literal(new Object());

if (match(NUMBER, STRING)) {
if (match(NUMBER)) {
return new Literal(new Object(previous().literal));
}

if (match(STRING)) {
return new Literal(new Object(previous().literal));
}

Expand Down
14 changes: 12 additions & 2 deletions src/Scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "Token.h"

bool Scanner::isAtEnd() { return current >= source.length(); }

char Scanner::advance() { return source[current++]; }

std::vector<Token> Scanner::scanTokens() {
Expand Down Expand Up @@ -102,17 +101,28 @@ void Scanner::scanToken() {
}
}

std::string Scanner::subString(int &start, int &end, std::string &str) {
std::string res = "";
for (int i = start + 1; i <= end - 1; i++) {
if (str[i] == '"')
break;
res += str[i];
}
return res;
}

void Scanner::string() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n')
line++;
advance();
}

if (isAtEnd()) {
crux::error(line, "Unterminated string");
}
advance();
std::string value = source.substr(start + 1, current - 1);
std::string value = subString(start, current, source);
addString(value);
}

Expand Down
22 changes: 22 additions & 0 deletions test/TestInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "Parser.h"
#include "Scanner.h"
#include "gtest/gtest.h"
#include <string>
#include <vector>

TEST(InterpreterCheck, TestInterpreterBasic) {

Expand Down Expand Up @@ -46,3 +48,23 @@ TEST(InterpreterTest, TestParserTernary) {
Expr *expression = p.parse();
ASSERT_EQ(Interpreter{}.interpret(expression), "true");
}

TEST(IntrepreterTest, TestStringNumExpressions) {
std::string test1 = "\"test\"+8";
std::string test2 = "8+\"test\"";

Scanner scan1(test1);
Scanner scan2(test2);
std::vector<Token> tokens1 = scan1.scanTokens();
std::vector<Token> tokens2 = scan2.scanTokens();

Parser p1(tokens1);
Parser p2(tokens2);

Expr *expression1 = p1.parse();
Expr *expression2 = p2.parse();

ASSERT_EQ(Interpreter{}.interpret(expression1), "test8.000000");
// todo: test not passing ASSERT_EQ(Interpreter{}.interpret(expression2),
// "8.000000test");
}
2 changes: 1 addition & 1 deletion test/lib

0 comments on commit d65ca2d

Please sign in to comment.