Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeemin committed Aug 7, 2024
1 parent e3cc54b commit f5ff4e0
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 54 deletions.
29 changes: 27 additions & 2 deletions generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const GeneratorOperSpec Generator::m_operspecs[] =
{ "=>", &Generator::GenerateOperGreaterOrEqual },
{ "AND", &Generator::GenerateOperAnd },
{ "OR", &Generator::GenerateOperOr },
//TODO: XOR
{ "XOR", &Generator::GenerateOperXor },
//TODO: EQV
//TODO: IMP
};
Expand All @@ -90,6 +90,7 @@ const GeneratorFuncSpec Generator::m_funcspecs[] =
{ KeywordINP, &Generator::GenerateFuncInp },
{ KeywordLEN, &Generator::GenerateFuncLen },
{ KeywordINKEY, &Generator::GenerateFuncInkey },
{ KeywordPOS, &Generator::GenerateFuncPos },
};


Expand Down Expand Up @@ -1190,6 +1191,14 @@ void Generator::GenerateOperOr(const ExpressionModel& expr, const ExpressionNode
m_final->AddComment("TODO operation OR");
}

void Generator::GenerateOperXor(const ExpressionModel& expr, const ExpressionNode& node, const ExpressionNode& nodeleft, const ExpressionNode& noderight)
{
//const string comment = "\t; Operation \'XOR\'";

//TODO
m_final->AddComment("TODO operation XOR");
}


// Function generation ///////////////////////////////////////////////

Expand Down Expand Up @@ -1256,8 +1265,9 @@ void Generator::GenerateFuncLen(const ExpressionModel& expr, const ExpressionNod
const ExpressionModel& expr1 = node.args[0];
GenerateExpression(expr1);

m_final->AddLine("\tMOV\tR0, R1\t");
m_final->AddLine("\tCLR\tR0\t");
m_final->AddLine("\tBISB\t(R0), R0\t; LEN");
m_final->AddLine("\tBISB\t(R1), R0\t; LEN"); // get byte of the string length
}

void Generator::GenerateFuncInkey(const ExpressionModel& expr, const ExpressionNode& node)
Expand All @@ -1266,5 +1276,20 @@ void Generator::GenerateFuncInkey(const ExpressionModel& expr, const ExpressionN
m_final->AddComment("TODO INKEY$");
}

void Generator::GenerateFuncPos(const ExpressionModel& expr, const ExpressionNode& node)
{
assert(node.args.size() <= 1);

// If we have non-const expression then calculate it
if (node.args.size() > 0)
{
const ExpressionModel& expr1 = node.args[0];
if (!expr.IsConstExpression())
GenerateExpression(expr1);
}

m_final->AddComment("TODO POS");
}


//////////////////////////////////////////////////////////////////////
2 changes: 2 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,13 @@ class Generator
void GenerateOperGreaterOrEqual(const ExpressionModel& expr, const ExpressionNode& node, const ExpressionNode& nodeleft, const ExpressionNode& noderight);
void GenerateOperAnd(const ExpressionModel& expr, const ExpressionNode& node, const ExpressionNode& nodeleft, const ExpressionNode& noderight);
void GenerateOperOr(const ExpressionModel& expr, const ExpressionNode& node, const ExpressionNode& nodeleft, const ExpressionNode& noderight);
void GenerateOperXor(const ExpressionModel& expr, const ExpressionNode& node, const ExpressionNode& nodeleft, const ExpressionNode& noderight);
private:
void GenerateFuncAbs(const ExpressionModel& expr, const ExpressionNode& node);
void GenerateFuncRnd(const ExpressionModel& expr, const ExpressionNode& node);
void GenerateFuncPeek(const ExpressionModel& expr, const ExpressionNode& node);
void GenerateFuncInp(const ExpressionModel& expr, const ExpressionNode& node);
void GenerateFuncLen(const ExpressionModel& expr, const ExpressionNode& node);
void GenerateFuncInkey(const ExpressionModel& expr, const ExpressionNode& node);
void GenerateFuncPos(const ExpressionModel& expr, const ExpressionNode& node);
};
10 changes: 7 additions & 3 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ void Parser::ParseColor(StatementModel& statement)
if (!token.IsComma())
MODEL_ERROR(MSG_UNEXPECTED);

//NOTE: Documentation tells about optional third parameter for border color, not implemented on UKNC
token = PeekNextTokenSkipDivider();
ExpressionModel expr3 = ParseExpression();
CHECK_MODEL_ERROR;
Expand Down Expand Up @@ -1382,16 +1383,18 @@ void Parser::ParsePoke(StatementModel& statement)

void Parser::ParsePsetPreset(StatementModel& statement)
{
Token token = GetNextTokenSkipDivider();
Token token = PeekNextTokenSkipDivider();
if ((token.type == TokenTypeSymbol && token.symbol == '@') ||
(token.IsKeyword(KeywordSTEP)))
{
GetNextToken();
statement.relative = true;
token = GetNextTokenSkipDivider();
token = PeekNextTokenSkipDivider();
}

if (!token.IsOpenBracket())
MODEL_ERROR(MSG_OPEN_BRACKET_EXPECTED);
GetNextToken();

token = PeekNextTokenSkipDivider();
ExpressionModel expr1 = ParseExpression();
Expand Down Expand Up @@ -1816,9 +1819,10 @@ void Parser::ParseDefUsr(StatementModel& statement)

void Parser::ParseScreen(StatementModel& statement)
{
Token token = GetNextTokenSkipDivider();
Token token = PeekNextTokenSkipDivider();
if (token.type != TokenTypeNumber)
MODEL_ERROR("Numeric argument expected.");
GetNextToken();

statement.params.push_back(token);

Expand Down
7 changes: 6 additions & 1 deletion tests/0009-const-expr-str.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
----------------------------------------------------------------------
10 CLS
100 REM Expressions with strings
105 PRINT "5", LEN("AAAAA")
110 PRINT "65", ASC("A")
111 PRINT "A", CHR$(65)
112 PRINT "65", ASC(CHR$(65))
Expand All @@ -12,7 +13,11 @@
126 PRINT "D431", HEX$(-11215)
130 PRINT "0", OCT$(0)
131 PRINT "152061", OCT$(-11215)
140 PRINT "AB", "A" + "B"
135 PRINT "-12345", STR$(-12345)
150 PRINT "AB", "A" + "B"
160 PRINT "..........", STRING$(10, ".")
162 PRINT "AAAAAAAAAA", STRING$(10, 65)
170 PRINT "234", MID$("1234567890", 2, 3)
200 REM Compare strings
210 PRINT "-1", "A" = "A"
212 PRINT "0", "A" = "B"
Expand Down
100 changes: 83 additions & 17 deletions tests/0029-syntax-err.test
Original file line number Diff line number Diff line change
@@ -1,22 +1,88 @@
-q
----------------------------------------------------------------------
10 CLS
20 CLS 'comment
210 CLS 0
220 CLS A%
230 CLS .
240 CLS;
12 CLS 'comment
14 CLS 0
16 CLS A%
18 CLS .
20 CLS;
30 COLOR
32 COLOR ,,
33 SCREEN 1
34 SCREEN
35 SCREEN 'comment
36 SCREEN "A"
37 SCREEN A$
40 LOCATE 1,2, 1
41 LOCATE 1,2 'comment
42 LOCATE
43 LOCATE .
44 LOCATE ,
45 LOCATE ,,
100 PSET (150, 200), 1
101 PSET @ (150, 200), 1
102 PSET STEP (150, 200), 1
103 PSET (150, 200)
104 PSET (150, 200) 'comment
106 PSET @
107 PSET STEP
108 PSET 150, 200
109 PSET A$
110 PSET (150, 200), 1,
111 PSET (150, 200), 1,2
----------------------------------------------------------------------
ERROR at 3:9 line 210 - Unexpected text at the end of the statement.
210 CLS 0
^
ERROR at 4:9 line 220 - Unexpected text at the end of the statement.
220 CLS A%
^
ERROR at 5:9 line 230 - Unexpected text at the end of the statement.
230 CLS .
^
ERROR at 6:8 line 240 - Unexpected text at the end of the statement.
240 CLS;
ERROR at 3:8 line 14 - Unexpected text at the end of the statement.
14 CLS 0
^
Parsing ERRORS: 4
ERROR at 4:8 line 16 - Unexpected text at the end of the statement.
16 CLS A%
^
ERROR at 5:8 line 18 - Unexpected text at the end of the statement.
18 CLS .
^
ERROR at 6:7 line 20 - Unexpected text at the end of the statement.
20 CLS;
^
ERROR at 7:9 line 30 - Arguments expected.
30 COLOR
^
ERROR at 8:12 line 32 - Expression should not be empty.
32 COLOR ,,
^
ERROR at 10:10 line 34 - Numeric argument expected.
34 SCREEN
^
ERROR at 11:11 line 35 - Numeric argument expected.
35 SCREEN 'comment
^
ERROR at 12:11 line 36 - Numeric argument expected.
36 SCREEN "A"
^
ERROR at 13:11 line 37 - Numeric argument expected.
37 SCREEN A$
^
ERROR at 16:10 line 42 - Arguments expected.
42 LOCATE
^
ERROR at 19:13 line 45 - Expression should not be empty.
45 LOCATE ,,
^
ERROR at 25:11 line 106 - Open bracket expected.
106 PSET @
^
ERROR at 26:14 line 107 - Open bracket expected.
107 PSET STEP
^
ERROR at 27:10 line 108 - Open bracket expected.
108 PSET 150, 200
^
ERROR at 28:10 line 109 - Open bracket expected.
109 PSET A$
^
ERROR at 29:23 line 110 - Unexpected text at the end of the statement.
110 PSET (150, 200), 1,
^
ERROR at 30:23 line 111 - Unexpected text at the end of the statement.
111 PSET (150, 200), 1,2
^
Parsing ERRORS: 18
62 changes: 55 additions & 7 deletions tests/0029-validation.test
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
-q
----------------------------------------------------------------------
100 GOTO 65535
110 POKE 0%, "AAA"
120 POKE "AAA", 0
10 CLEAR "A"
50 COLOR "A",2
51 COLOR A$,2
52 COLOR 1,"B"
53 COLOR 1,B$
54 COLOR -1,2
55 COLOR 9,2
56 COLOR 1,-1
57 COLOR 1,9
66 LOCATE "A"
67 LOCATE A$
68 LOCATE -1
69 LOCATE 256
70 LOCATE ,-1
71 LOCATE ,256
72 LOCATE ,, "A"
73 LOCATE ,, A$
82 PSET (A$, 200)
83 PSET (100, B$)
84 PSET (150, 200), A$
95 KEY "A", "B"
96 KEY 1, 2
97 KEY 11, "AAA"
100 NEXT
110 GOTO 65535
120 POKE 0%, "AAA"
122 POKE "AAA", 0
----------------------------------------------------------------------
ERROR in line 100 - Invalid line number 65535.
ERROR in line 110 at 2:14 - Expression should be of type Integer or Single.
ERROR in line 120 at 3:10 - Expression should be of type Integer or Single.
Validation ERRORS: 3
ERROR in line 10 at 1:10 - Expression should be of type Integer or Single.
ERROR in line 50 at 2:10 - Expression should be of type Integer or Single.
ERROR in line 51 at 3:10 - Expression should be of type Integer or Single.
ERROR in line 52 at 4:12 - Expression should be of type Integer or Single.
ERROR in line 53 at 5:12 - Expression should be of type Integer or Single.
ERROR in line 54 - Parameter value (-1) is out of range 1..8.
ERROR in line 55 - Parameter value (9) is out of range 1..8.
ERROR in line 56 - Parameter value (-1) is out of range 1..8.
ERROR in line 57 - Parameter value (9) is out of range 1..8.
ERROR in line 66 at 10:11 - Expression should be of type Integer or Single.
ERROR in line 67 at 11:11 - Expression should be of type Integer or Single.
ERROR in line 68 - Parameter value (-1) is out of range 0..255.
ERROR in line 69 - Parameter value (256) is out of range 0..255.
ERROR in line 70 - Parameter value (-1) is out of range 0..255.
ERROR in line 71 - Parameter value (256) is out of range 0..255.
ERROR in line 72 at 16:14 - Expression should be of type Integer or Single.
ERROR in line 73 at 17:14 - Expression should be of type Integer or Single.
ERROR in line 82 at 18:10 - Expression should be of type Integer or Single.
ERROR in line 83 at 19:15 - Expression should be of type Integer or Single.
ERROR in line 84 at 20:21 - Expression should be of type Integer or Single.
ERROR in line 95 at 21:8 - Expression should be of type Integer or Single.
ERROR in line 96 at 22:11 - Expression should be of type String.
ERROR in line 97 - Parameter value (11) is out of range 1..10.
ERROR in line 100 - NEXT without FOR.
ERROR in line 110 - Invalid line number 65535.
ERROR in line 120 at 26:14 - Expression should be of type Integer or Single.
ERROR in line 122 at 27:10 - Expression should be of type Integer or Single.
Validation ERRORS: 27
9 changes: 9 additions & 0 deletions tests/0105-csrlin-pos.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-q
----------------------------------------------------------------------
1000 X=POS
1010 Y=CSRLIN
1020 LOCATE X1,Y1
1030 GOSUB 2000
1040 STOP
2000 LOCATE X,Y
2100 RETURN
Loading

0 comments on commit f5ff4e0

Please sign in to comment.