From 09016f7e2ece0435b042119622e0024a0784f68c Mon Sep 17 00:00:00 2001 From: Mikael Pettersson Date: Sun, 12 Jan 2020 21:00:27 +0100 Subject: [PATCH] as: parse: handle symbol +/- offset exprs --- erlang/apps/as/src/parse.erl | 23 ++++++++++++++++++++--- erlang/apps/as/src/scan.erl | 3 ++- erlang/apps/as/src/token.erl | 1 + erlang/apps/as/src/token.hrl | 2 ++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/erlang/apps/as/src/parse.erl b/erlang/apps/as/src/parse.erl index a0fb153..f1d4eee 100644 --- a/erlang/apps/as/src/parse.erl +++ b/erlang/apps/as/src/parse.erl @@ -639,16 +639,33 @@ do_expr(ScanState, First) -> {ok, {_Location, {?T_UINTEGER, UInt}}} -> {ok, mk_integer_expr(UInt)}; {ok, {_Location, {?T_LOCAL_LABEL, Number, Direction}}} -> - {ok, mk_local_label_expr(Number, Direction)}; + do_expr_maybe_offset(ScanState, _Symbol = {Number, Direction}); {ok, {_Location, {?T_SYMBOL, Symbol}}} -> - {ok, mk_symbol_expr(Symbol)}; + do_expr_maybe_offset(ScanState, Symbol); _ -> badtok("invalid start of expr", First) end. +do_expr_maybe_offset(ScanState, Symbol) -> + case scan:token(ScanState) of + {ok, {_Location, ?T_MINUS}} -> do_expr_offset(ScanState, Symbol, _IsMinus = true); + {ok, {_Location, ?T_PLUS}} -> do_expr_offset(ScanState, Symbol, _IsMinus = false); + {ok, {_Location, _Token} = First} -> + scan:pushback(ScanState, First), + {ok, mk_symbol_expr(Symbol)}; + {error, _Reason} = Error -> Error + end. + +do_expr_offset(ScanState, Symbol, IsMinus) -> + case scan:token(ScanState) of + {ok, {_Location, {?T_UINTEGER, UInt}}} -> + {ok, mk_symbol_expr(Symbol, if IsMinus -> -UInt; true -> UInt end)}; + ScanRes -> badtok("expected after ", ScanRes) + end. + mk_integer_expr(Value) -> #expr{symbol = false, offset = Value}. -mk_local_label_expr(Number, Direction) -> mk_symbol_expr({Number, Direction}). mk_symbol_expr(Symbol) -> #expr{symbol = Symbol, offset = 0}. +mk_symbol_expr(Symbol, Offset) -> #expr{symbol = Symbol, offset = Offset}. %% String Lists ---------------------------------------------------------------- diff --git a/erlang/apps/as/src/scan.erl b/erlang/apps/as/src/scan.erl index d955140..87c0ac5 100644 --- a/erlang/apps/as/src/scan.erl +++ b/erlang/apps/as/src/scan.erl @@ -1,7 +1,7 @@ %%% -*- erlang-indent-level: 2 -*- %%% %%% scanner for pdp10-elf as -%%% Copyright (C) 2013-2019 Mikael Pettersson +%%% Copyright (C) 2013-2020 Mikael Pettersson %%% %%% This file is part of pdp10-tools. %%% @@ -169,6 +169,7 @@ do_token(ScanState) -> $/ -> do_slash(ScanState); $\" -> do_string(ScanState, Location, []); $- -> {ok, {Location, ?T_MINUS}}; + $+ -> {ok, {Location, ?T_PLUS}}; _ -> if $0 =< Ch, Ch =< $9 -> do_number(ScanState, Location, Ch); ($A =< Ch andalso Ch =< $Z) orelse diff --git a/erlang/apps/as/src/token.erl b/erlang/apps/as/src/token.erl index f595d55..457c5b9 100644 --- a/erlang/apps/as/src/token.erl +++ b/erlang/apps/as/src/token.erl @@ -84,6 +84,7 @@ format(Token) -> ?T_DOT -> "."; ?T_LPAREN -> "("; ?T_MINUS -> "-"; + ?T_PLUS -> "+"; ?T_RPAREN -> ")"; ?T_NEWLINE -> ""; ?T_EOF -> "" diff --git a/erlang/apps/as/src/token.hrl b/erlang/apps/as/src/token.hrl index 51f48cb..5b31d9f 100644 --- a/erlang/apps/as/src/token.hrl +++ b/erlang/apps/as/src/token.hrl @@ -61,6 +61,7 @@ -define(T_DOT, 'T_DOT'). % . -define(T_LPAREN, 'T_LPAREN'). % ( -define(T_MINUS, 'T_MINUS'). % - +-define(T_PLUS, 'T_PLUS'). % + -define(T_RPAREN, 'T_RPAREN'). % ) %% synthetic symbols @@ -96,6 +97,7 @@ | ?T_DOT | ?T_LPAREN | ?T_MINUS + | ?T_PLUS | ?T_RPAREN | ?T_NEWLINE | ?T_EOF