Skip to content

Commit faf9c1c

Browse files
committed
implement exponentiation operator
1 parent c4de85a commit faf9c1c

File tree

13 files changed

+587
-382
lines changed

13 files changed

+587
-382
lines changed

LANG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# tcbasic Language Reference
22

3+
## Operators
4+
5+
### Arithmetic Operators
6+
7+
- `+` - addition
8+
- `-` - subtraction
9+
- `*` - multiplication
10+
- `/` - division
11+
- `\` - integer division
12+
- `%` - modulus
13+
- `^` - exponentiation
14+
15+
### Relational Operators
16+
17+
- `<=` - less than or equal to
18+
- `<` - less than
19+
- `=` - equal to
20+
- `<>` - not equal to
21+
- `><` - not equal to
22+
- `>` - greater than
23+
- `>=` - greater than or equal to
24+
325
## Statements
426

527
### REM remark

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Execute a program from a file in batch mode:
5151
1002 REM Uses Z as a temporary variable.
5252
1050 LET Y = 0.5 * X
5353
1100 LET Z = Y
54-
1200 LET Y = Y-(((Y*Y)-X)/(2*Y))
54+
1200 LET Y = Y-(((Y^2)-X)/(2*Y))
5555
1300 IF Z <> Y THEN GOTO 1100
5656
1400 RETURN
5757

TODO.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ Implement additional statements:
1010
* DEF
1111
* DIM
1212

13-
Implement additional operators:
14-
15-
* ^ operator (x^y means x to the power of y) -- remember to get order of operations right.
16-
* Document all operators (+,-,*,/,MOD,etc)
17-
1813
Implement language fundamentals:
1914

2015
* Strings
@@ -39,6 +34,3 @@ Other changes:
3934

4035
* Better error handling / reporting.
4136

42-
Report and Fix Issues:
43-
44-
* https://github.com/tcort/tcbasic/issues

include/factor.h

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,16 @@
1919
#ifndef __FACTOR_H
2020
#define __FACTOR_H
2121

22-
struct expression;
22+
struct tokenizer;
23+
struct primary;
2324
struct number;
24-
struct rnd;
25-
struct time;
26-
struct var;
27-
struct sin;
28-
struct cos;
29-
struct tan;
30-
struct cot;
31-
struct atn;
32-
struct exp;
33-
struct log;
34-
struct abs;
35-
struct sgn;
36-
struct sqr;
37-
struct int_;
3825

3926
struct factor {
40-
int type;
41-
union factor_union {
42-
struct expression *e;
43-
struct number *n;
44-
struct rnd *r;
45-
struct time *time;
46-
struct sin *sin;
47-
struct cos *cos;
48-
struct tan *tan;
49-
struct cot *cot;
50-
struct atn *atn;
51-
struct exp *exp;
52-
struct log *log;
53-
struct abs *abs;
54-
struct sgn *sgn;
55-
struct sqr *sqr;
56-
struct int_ *int_;
57-
struct var *v;
58-
} u;
27+
struct primary *p1;
28+
struct primary *p2;
5929
};
6030

61-
struct factor *new_factor(int type, void *value);
31+
struct factor *new_factor(struct primary *p1, struct primary *p2);
6232
struct factor *parse_factor(struct tokenizer *t);
6333
struct number *eval_factor(struct factor *f);
6434
void print_factor(struct factor *f);

include/number.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct number *multiply_number(struct number *x, struct number *y);
4545
struct number *divide_number(struct number *x, struct number *y);
4646
struct number *idivide_number(struct number *x, struct number *y);
4747
struct number *modulus_number(struct number *x, struct number *y);
48+
struct number *pow_number(struct number *x, struct number *y);
4849
void print_number(struct number *n);
4950
void free_number(struct number *n);
5051

include/primary.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
tcbasic - a small BASIC Interpreter written in C.
3+
Copyright (C) 2015, 2016, 2017, 2018, 2020 Thomas Cort <[email protected]>
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef __PRIMARY_H
20+
#define __PRIMARY_H
21+
22+
struct expression;
23+
struct number;
24+
struct rnd;
25+
struct time;
26+
struct var;
27+
struct sin;
28+
struct cos;
29+
struct tan;
30+
struct cot;
31+
struct atn;
32+
struct exp;
33+
struct log;
34+
struct abs;
35+
struct sgn;
36+
struct sqr;
37+
struct int_;
38+
39+
struct primary {
40+
int type;
41+
union primary_union {
42+
struct expression *e;
43+
struct number *n;
44+
struct rnd *r;
45+
struct time *time;
46+
struct sin *sin;
47+
struct cos *cos;
48+
struct tan *tan;
49+
struct cot *cot;
50+
struct atn *atn;
51+
struct exp *exp;
52+
struct log *log;
53+
struct abs *abs;
54+
struct sgn *sgn;
55+
struct sqr *sqr;
56+
struct int_ *int_;
57+
struct var *v;
58+
} u;
59+
};
60+
61+
struct primary *new_primary(int type, void *value);
62+
struct primary *parse_primary(struct tokenizer *t);
63+
struct number *eval_primary(struct primary *f);
64+
void print_primary(struct primary *f);
65+
void free_primary(struct primary *f);
66+
67+
#endif

include/tokenizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum token_types {
2626
SIN, COS, TAN, COT, ATN, EXP, LOG, ABS, SGN, SQR, INT_,
2727
LTEQ, LTGT, LT, GTEQ, GTLT, GT, EQ,
2828
PLUS, MINUS, TIMES, DIVIDE, IDIVIDE, MOD,
29-
COMMA, OPAREN, CPAREN,
29+
CIRCUMFLEX, COMMA, OPAREN, CPAREN,
3030
STR, VAR, NUMBER,
3131
SPACE, INVALID, EOS
3232
};

0 commit comments

Comments
 (0)