lingua-franca 0.10.1
Lingua Franca code generator
Loading...
Searching...
No Matches
org.lflang.generator.CodeBuilder Class Reference

Helper class for printing code with indentation. More...

Public Member Functions

 CodeBuilder ()
 Construct a new empty code emitter.
 CodeBuilder (CodeBuilder model)
 Construct a new code emitter with the text and indentation of the specified code emitter.
void endChannelIteration (PortInstance port)
 If the specified port is a multiport, then start a specified iteration over the channels of the multiport using as the channel index the variable name returned by CUtil#channelIndex(PortInstance).
void endScopedBankChannelIteration (PortInstance port, String count)
 End a scoped block to iterate over bank members and channels for the specified port with a variable with the name given by count counting the iterations.
void endScopedBlock ()
void endScopedRangeBlock (RuntimeRange< PortInstance > range)
 End a scoped block for the specified range.
void endScopedRangeBlock (SendRange srcRange, RuntimeRange< PortInstance > dstRange)
 End a scoped block that iterates over the specified pair of ranges.
String getCode ()
 Get the code produced so far.
void indent ()
 Increase the indentation of the output code produced.
void insert (int position, String text)
 Insert the specified text at the specified position.
int length ()
 Return the length of the code in characters.
void newLine ()
 Add a new line.
void pr (CharSequence text)
 Append the given text to the code buffer at the current indentation level.
void pr (String format, Object... args)
 Append the specified text plus a final newline.
void prComment (String comment)
 Append a single-line, C-style comment to the code.
void prEndSourceLineNumber (boolean suppress)
 Print a tag marking the end of a block corresponding to the source LF file.
void prSourceLineNumber (EObject eObject, boolean suppress)
 Print the #line compiler directive with the line number of the specified object.
CodeBuilder removeLines (String prefix)
 Remove all lines that start with the specified prefix and return a new CodeBuilder with the result.
void startChannelIteration (PortInstance port)
 If the specified port is a multiport, then start a specified iteration over the channels of the multiport using as the channel index the variable name returned by CUtil#channelIndex(PortInstance).
void startScopedBankChannelIteration (PortInstance port, String count)
 Start a scoped block to iterate over bank members and channels for the specified port with a variable with the name given by count counting the iterations.
void startScopedBlock ()
 Start a scoped block, which is a section of code surrounded by curley braces and indented.
void startScopedBlock (ReactorInstance reactor)
 Start a scoped block for the specified reactor.
void startScopedRangeBlock (RuntimeRange< PortInstance > range, String runtimeIndex, String bankIndex, String channelIndex, boolean nested)
 Start a scoped block that iterates over the specified range of port channels.
void startScopedRangeBlock (SendRange srcRange, RuntimeRange< PortInstance > dstRange)
 Start a scoped block that iterates over the specified pair of ranges.
String toString ()
 Return the code as a string.
void unindent ()
 Reduce the indentation by one level for generated code/.
CodeMap writeToFile (String path) throws IOException
 Write the text to a file.

Detailed Description

Helper class for printing code with indentation.

This class is backed by a StringBuilder and is used to accumulate code to be printed to a file. Its main function is to handle indentation.

Author
Edward A. Lee
Peter Donovan

Constructor & Destructor Documentation

◆ CodeBuilder() [1/2]

org.lflang.generator.CodeBuilder.CodeBuilder ( )

Construct a new empty code emitter.

◆ CodeBuilder() [2/2]

org.lflang.generator.CodeBuilder.CodeBuilder ( CodeBuilder model)

Construct a new code emitter with the text and indentation of the specified code emitter.

Parameters
modelThe model code emitter.

Member Function Documentation

◆ endChannelIteration()

void org.lflang.generator.CodeBuilder.endChannelIteration ( PortInstance port)

If the specified port is a multiport, then start a specified iteration over the channels of the multiport using as the channel index the variable name returned by CUtil#channelIndex(PortInstance).

If the port is not a multiport, do nothing.

Parameters
portThe port.

◆ endScopedBankChannelIteration()

void org.lflang.generator.CodeBuilder.endScopedBankChannelIteration ( PortInstance port,
String count )

End a scoped block to iterate over bank members and channels for the specified port with a variable with the name given by count counting the iterations.

Parameters
portThe port.
countThe variable name to use for the counter, or null to not provide a counter.

◆ endScopedBlock()

void org.lflang.generator.CodeBuilder.endScopedBlock ( )

◆ endScopedRangeBlock() [1/2]

void org.lflang.generator.CodeBuilder.endScopedRangeBlock ( RuntimeRange< PortInstance > range)

End a scoped block for the specified range.

Parameters
rangeThe send range.

◆ endScopedRangeBlock() [2/2]

void org.lflang.generator.CodeBuilder.endScopedRangeBlock ( SendRange srcRange,
RuntimeRange< PortInstance > dstRange )

End a scoped block that iterates over the specified pair of ranges.

Parameters
srcRangeThe send range.
dstRangeThe destination range.

◆ getCode()

String org.lflang.generator.CodeBuilder.getCode ( )

Get the code produced so far.

Returns
The code produced so far as a String.

◆ indent()

void org.lflang.generator.CodeBuilder.indent ( )

Increase the indentation of the output code produced.

◆ insert()

void org.lflang.generator.CodeBuilder.insert ( int position,
String text )

Insert the specified text at the specified position.

Parameters
positionThe position.
textThe text.

◆ length()

int org.lflang.generator.CodeBuilder.length ( )

Return the length of the code in characters.

◆ newLine()

void org.lflang.generator.CodeBuilder.newLine ( )

Add a new line.

◆ pr() [1/2]

void org.lflang.generator.CodeBuilder.pr ( CharSequence text)

Append the given text to the code buffer at the current indentation level.

◆ pr() [2/2]

void org.lflang.generator.CodeBuilder.pr ( String format,
Object... args )

Append the specified text plus a final newline.

Parameters
formatA format string to be used by String.format or the text to append if no further arguments are given.
argsAdditional arguments to pass to the formatter.

◆ prComment()

void org.lflang.generator.CodeBuilder.prComment ( String comment)

Append a single-line, C-style comment to the code.

Parameters
commentThe comment.

◆ prEndSourceLineNumber()

void org.lflang.generator.CodeBuilder.prEndSourceLineNumber ( boolean suppress)

Print a tag marking the end of a block corresponding to the source LF file.

Parameters
suppressDo nothing if true.

◆ prSourceLineNumber()

void org.lflang.generator.CodeBuilder.prSourceLineNumber ( EObject eObject,
boolean suppress )

Print the #line compiler directive with the line number of the specified object.

Parameters
eObjectThe node.
suppressDo nothing if true.

◆ removeLines()

CodeBuilder org.lflang.generator.CodeBuilder.removeLines ( String prefix)

Remove all lines that start with the specified prefix and return a new CodeBuilder with the result.

Parameters
prefixThe prefix.

◆ startChannelIteration()

void org.lflang.generator.CodeBuilder.startChannelIteration ( PortInstance port)

If the specified port is a multiport, then start a specified iteration over the channels of the multiport using as the channel index the variable name returned by CUtil#channelIndex(PortInstance).

If the port is not a multiport, do nothing. This is required to be followed by endChannelIteration(PortInstance).

Parameters
portThe port.

◆ startScopedBankChannelIteration()

void org.lflang.generator.CodeBuilder.startScopedBankChannelIteration ( PortInstance port,
String count )

Start a scoped block to iterate over bank members and channels for the specified port with a variable with the name given by count counting the iterations.

If this port is a multiport, then the channel index variable name is that returned by CUtil#channelIndex(PortInstance).

This block is intended to be nested, where each block is put within a similar block for the reactor's parent.

This is required to be followed by a call to endScopedBankChannelIteration(PortInstance, String).

Parameters
portThe port.
countThe variable name to use for the counter, or null to not provide a counter.

◆ startScopedBlock() [1/2]

void org.lflang.generator.CodeBuilder.startScopedBlock ( )

Start a scoped block, which is a section of code surrounded by curley braces and indented.

This must be followed by an endScopedBlock().

◆ startScopedBlock() [2/2]

void org.lflang.generator.CodeBuilder.startScopedBlock ( ReactorInstance reactor)

Start a scoped block for the specified reactor.

If the reactor is a bank, then this starts a for loop that iterates over the bank members using a standard index variable whose name is that returned by CUtil#bankIndex(ReactorInstance). If the reactor is null or is not a bank, then this simply starts a scoped block by printing an opening curly brace. This also adds a declaration of a pointer to the self struct of the reactor or bank member.

This block is intended to be nested, where each block is put within a similar block for the reactor's parent. This ensures that all (possibly nested) bank index variables are defined within the block.

This must be followed by an endScopedBlock().

Parameters
reactorThe reactor instance.

◆ startScopedRangeBlock() [1/2]

void org.lflang.generator.CodeBuilder.startScopedRangeBlock ( RuntimeRange< PortInstance > range,
String runtimeIndex,
String bankIndex,
String channelIndex,
boolean nested )

Start a scoped block that iterates over the specified range of port channels.

This must be followed by a call to endScopedRangeBlock.

This block should NOT be nested, where each block is put within a similar block for the reactor's parent. Within the created block, every use of CUtil#reactorRef(ReactorInstance, String) must provide the second argument, a runtime index variable name, that must match the runtimeIndex parameter given here.

Parameters
rangeThe range of port channels.
runtimeIndexA variable name to use to index the runtime instance of either port's parent or the port's parent's parent (if nested is true), or null to use the default, "runtime_index".
bankIndexA variable name to use to index the bank of the port's parent or null to use the default, the string returned by CUtil#bankIndexName(ReactorInstance).
channelIndexA variable name to use to index the channel or null to use the default, the string returned by CUtil#channelIndexName(PortInstance).
nestedIf true, then the runtimeIndex variable will be set to the bank index of the port's parent's parent rather than the port's parent.

◆ startScopedRangeBlock() [2/2]

void org.lflang.generator.CodeBuilder.startScopedRangeBlock ( SendRange srcRange,
RuntimeRange< PortInstance > dstRange )

Start a scoped block that iterates over the specified pair of ranges.

The destination range can be wider than the source range, in which case the source range is reused until the destination range is filled. The following integer variables will be defined within the scoped block:

  • src_channel: The channel index for the source.
  • src_bank: The bank index of the source port&#39;s parent.
  • src_runtime: The runtime index of the source port&#39;s parent or the parent&#39;s parent (if the source is an input).
  • dst_channel: The channel index for the destination.
  • dst_bank: The bank index of the destination port&#39;s parent.
  • dst_runtime: The runtime index of the destination port&#39;s parent or the parent&#39;s parent (if destination is an output).

For convenience, the above variable names are defined in the private class variables sc, sb, sr, and dc, db, dr.

This block should NOT be nested, where each block is put within a similar block for the reactor's parent. Within the created block, every use of CUtil#reactorRef(ReactorInstance, String) and related functions must provide the above variable names.

This must be followed by a call to endScopedRangeBlock.

Parameters
srcRangeThe send range.
dstRangeThe destination range.

◆ toString()

String org.lflang.generator.CodeBuilder.toString ( )

Return the code as a string.

◆ unindent()

void org.lflang.generator.CodeBuilder.unindent ( )

Reduce the indentation by one level for generated code/.

◆ writeToFile()

CodeMap org.lflang.generator.CodeBuilder.writeToFile ( String path) throws IOException

Write the text to a file.

Parameters
pathThe file to write the code to.

The documentation for this class was generated from the following file:
  • /Users/runner/work/lingua-franca/lingua-franca/core/src/main/java/org/lflang/generator/CodeBuilder.java