Package mars.assembler.token
Class Token
java.lang.Object
mars.assembler.token.Token
Represents one token in the input MIPS program. Each Token carries, along with its
type and value, the position (line, column) in which its source appears in the MIPS program.
- Author:
- Pete Sanderson, August 2003; Sean Clarke, June 2024
-
Constructor Summary
ConstructorsConstructorDescriptionToken
(SourceLocation location, String literal, TokenType type, Object value) Create a newToken
with the given information. -
Method Summary
Modifier and TypeMethodDescriptioncopy()
Create a copy of this token.Get the underlying fragment of source code this token was created from.Get the location of this token in the source code.getType()
Get the type of this token.getValue()
Get the "value" this token represents, if any.boolean
Determine whether this token is a valid SPIM-style macro parameter.void
Modify the type of this token.void
Modify the "value" this token represents.Convert this token to anOperand
, if possible.toString()
Get the string representation of this token.
-
Constructor Details
-
Token
Create a newToken
with the given information.- Parameters:
location
- The location of this token in the source code.literal
- The underlying fragment of source code.type
- The type of this token.value
- The "value" of this token, dependent ontype
.- See Also:
-
-
Method Details
-
getType
Get the type of this token.- Returns:
- The type of this token.
-
setType
Modify the type of this token. This is typically used when the parsing stage of assembly reinterprets the token type, e.g. using an operator mnemonic as a label identifier.Note:
setValue(Object)
should be used alongside this method to ensure the token value remains valid.- Parameters:
type
- The new type for this token.
-
getValue
Get the "value" this token represents, if any. The object returned by this method is dependent on this token'stype
; the documentation for each variant ofTokenType
indicates what to expect from this method.- Returns:
- The token type-dependent value for this token.
-
setValue
Modify the "value" this token represents. The new value must follow the guidelines of this token'stype
.- Parameters:
value
- The new value for this token.
-
getLiteral
Get the underlying fragment of source code this token was created from.- Returns:
- The literal form of this token.
-
getLocation
Get the location of this token in the source code.- Returns:
- Name of file associated with this token.
-
toString
Get the string representation of this token. This method is equivalent togetLiteral()
. -
isSPIMStyleMacroParameter
public boolean isSPIMStyleMacroParameter()Determine whether this token is a valid SPIM-style macro parameter. MARS-style macro parameters start with%
, whereas SPIM-style macro parameters start with$
. Note that register names such as$zero
are not accepted.- Returns:
true
if this token is anIDENTIFIER
in the form of$
followed by at least one character, orfalse
otherwise.
-
toOperand
Convert this token to anOperand
, if possible. Only registers, integers, and characters can be converted using this method.- Returns:
- An operand based on the value of this token, or
null
if the conversion fails.
-
copy
Create a copy of this token.- Returns:
- A new instance with identical data.
-