Skip to content

Commit

Permalink
Optimize PRINT syntax, 4 less bytes.
Browse files Browse the repository at this point in the history
Also, new syntax is a little easier to understand.
  • Loading branch information
dmsc committed Jul 27, 2024
1 parent 95ae0c7 commit f3c8758
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/syntax/basic.syn
Original file line number Diff line number Diff line change
Expand Up @@ -285,35 +285,31 @@ STRING_FUNCTIONS:
ARRAY_STRING_ADDR emit TOK_DPEEK

# PRINT statement:
# - PRINT separator or an end-of-line if not given:
PRINT_SEP_EOL:
# - PRINT separators
PRINT_SEP:
"Tab" T_EXPR emit TOK_PRINT_TAB
# Right-TAB and COLOR are complicated because we need to apply it to the *next*
# parameter, so we read that parameter and the next optional separator:
"RTab" T_EXPR emit { TOK_BYTE_POKE, PRINT_RTAB_ARG } PRINT_ONE emit { TOK_PRINT_RTAB } PRINT_SEP_EOL
"Color" T_EXPR emit { TOK_BYTE_POKE, PRINT_COLOR } PRINT_MORE emit { TOK_0, TOK_BYTE_POKE, PRINT_COLOR }
"RTab" T_EXPR emit { TOK_BYTE_POKE, PRINT_RTAB_ARG } PRINT_ONE emit { TOK_PRINT_RTAB } PRINT_EOL
"Color" T_EXPR emit { TOK_BYTE_POKE, PRINT_COLOR } PRINT_NO_EOL emit { TOK_0, TOK_BYTE_POKE, PRINT_COLOR }
"," emit { TOK_BYTE, 10, TOK_PRINT_TAB }
";"
E_EOL emit { TOK_BYTE_PUT, ATEOL }

# - One item to print, converted to string:
PRINT_ONE_STR:
PRINT_ONE emit TOK_PRINT_STR
pass

# - One item to print, followed by a separator:
PRINT_ONE:
EXPR emit { TOK_INT_STR }
STR_EXPR

# - PRINT expression, including the optional I/O channel
PRINT_EXPR:
IO_CHAN_OPT PRINT_SEP_EOL PRINT_MORE
IO_CHAN_OPT PRINT_MORE
# - PRINT expression without adding an EOL at the end
PRINT_NO_EOL:
E_EOL
PRINT_SEP PRINT_NO_EOL
PRINT_ONE emit TOK_PRINT_STR PRINT_EOL

PRINT_MORE:
E_EOL IO_CHAN0
PRINT_ONE_STR PRINT_SEP_EOL PRINT_MORE
# - PRINT expression adding an EOL at the end
PRINT_EOL:
E_EOL emit { TOK_BYTE_PUT, ATEOL }
PRINT_NO_EOL

# Parse multi-line IF
THEN_OR_MULTILINE: 'THEN'
Expand Down Expand Up @@ -380,7 +376,7 @@ DIM_MORE: comma
# INPUT
INPUT_STR:
"\"" emit TOK_CSTRING E_CONST_STRING emit TOK_PRINT_STR PRINT_SEP_EOL # Prints a given string
"\"" emit TOK_CSTRING E_CONST_STRING emit TOK_PRINT_STR PRINT_SEP # Prints a given string
";" # If starts with ';' don't print anyting
emit { TOK_BYTE_PUT, 63 } # Prints a '?' by default

Expand Down Expand Up @@ -497,8 +493,8 @@ OPT_PROC_VAR:
# Parse a line
STATEMENT: statement
"." E_REM
"?" PRINT_EXPR
"PRInt" PRINT_EXPR
"?" IO_CHAN_OPT PRINT_EOL IO_CHAN0
"PRInt" IO_CHAN_OPT PRINT_EOL IO_CHAN0
"INput" INPUT_STR INPUT_VAR_LIST IO_CHAN0
"GEt" GETK_EXPR
"PUt" IO_CHAN_OPT EXPR emit TOK_PUT IO_CHAN0
Expand Down

0 comments on commit f3c8758

Please sign in to comment.