Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MIN and MAX functions #726

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/asm/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ static struct KeywordMapping {
{"SIZEOF", T_OP_SIZEOF},
{"STARTOF", T_OP_STARTOF},

{"MIN", T_OP_MIN},
{"MAX", T_OP_MAX},

{"ROUND", T_OP_ROUND},
{"CEIL", T_OP_CEIL},
{"FLOOR", T_OP_FLOOR},
Expand Down Expand Up @@ -595,7 +598,7 @@ struct KeywordDictNode {
uint16_t children[0x60 - ' '];
struct KeywordMapping const *keyword;
/* Since the keyword structure is invariant, the min number of nodes is known at compile time */
} keywordDict[365] = {0}; /* Make sure to keep this correct when adding keywords! */
} keywordDict[368] = {0}; /* Make sure to keep this correct when adding keywords! */

/* Convert a char into its index into the dict */
static uint8_t dictIndex(char c)
Expand Down
17 changes: 17 additions & 0 deletions src/asm/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ enum {
%type <constValue> uconst
%type <constValue> rs_uconst
%type <constValue> const_3bit
%type <constValue> min_args
%type <constValue> max_args
%type <expr> reloc_8bit
%type <expr> reloc_8bit_no_str
%type <expr> reloc_8bit_offset
Expand Down Expand Up @@ -537,6 +539,7 @@ enum {
%token T_OP_BANK "BANK"
%token T_OP_ALIGN "ALIGN"
%token T_OP_SIZEOF "SIZEOF" T_OP_STARTOF "STARTOF"
%token T_OP_MIN "MIN" T_OP_MAX "MAX"
%token T_OP_SIN "SIN" T_OP_COS "COS" T_OP_TAN "TAN"
%token T_OP_ASIN "ASIN" T_OP_ACOS "ACOS" T_OP_ATAN "ATAN" T_OP_ATAN2 "ATAN2"
%token T_OP_FDIV "FDIV"
Expand Down Expand Up @@ -1481,6 +1484,8 @@ relocexpr_no_str : scoped_anon_id { rpn_Symbol(&$$, $1); }

lexer_ToggleStringExpansion(true);
}
| T_OP_MIN T_LPAREN min_args T_RPAREN { rpn_Number(&$$, $3); }
| T_OP_MAX T_LPAREN max_args T_RPAREN { rpn_Number(&$$, $3); }
| T_OP_ROUND T_LPAREN const T_RPAREN {
rpn_Number(&$$, fix_Round($3));
}
Expand Down Expand Up @@ -1561,6 +1566,18 @@ const_no_str : relocexpr_no_str { $$ = rpn_GetConstVal(&$1); }
const_8bit : reloc_8bit { $$ = rpn_GetConstVal(&$1); }
;

min_args : const
| min_args T_COMMA const {
$$ = $1 < $3 ? $1 : $3;
}
;

max_args : const
| max_args T_COMMA const {
$$ = $1 > $3 ? $1 : $3;
}
;

string : T_STRING
| T_OP_STRSUB T_LPAREN string T_COMMA const T_COMMA uconst T_RPAREN {
size_t len = strlenUTF8($3);
Expand Down
2 changes: 2 additions & 0 deletions src/asm/rgbasm.5
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ String equates are not expanded within the parentheses.
.It Fn ISCONST arg Ta Returns 1 if Ar arg Ap s value is known by RGBASM (e.g. if it can be an argument to
.Ic IF ) ,
or 0 if only RGBLINK can compute its value.
.It Fn MIN args... Ta Returns the minimum operand, given at least one numeric operand.
.It Fn MAX args... Ta Returns the maximum operand, given at least one numeric operand.
.El
.Sh SECTIONS
Before you can start writing code, you must define a section.
Expand Down
8 changes: 8 additions & 0 deletions test/asm/math.asm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ ENDM
test (v 1) << (v 30) == (v $4000_0000)
test (v 2)**(v 30) == (v $4000_0000)

assert MIN(42) == 42
assert MIN(-10, 10) == -10
assert MIN(2, 1, 4, 3) == 1

assert MAX(42) == 42
assert MAX(-10, 10) == 10
assert MAX(2, 1, 4, 3) == 4

assert DIV(5.0, 2.0) == 2.5
assert DIV(-5.0, 2.0) == -2.5
assert DIV(-5.0, 0.0) == $8000_0000
Expand Down