16.11 Sling Assemblers

Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  16.   Parsing an Imperative Language


The Sling parser needs assemblers for each of its allowable commands and for each base element of the language. The base elements include numbers , variables , pi , the sliders s1 and s2 , negation, and the Sling functions. A single function assembler suffices to assemble all the different types of functions in Sling except for scale .

The scale function requires a special assembler because internally a Scale object takes five parameters in its constructor, and the Sling language allows for only two arguments in the scale function. The Sling parser always sets the first three parameters of a Scale object to , t , 1 .

The three command assemblers needed by the environment are for the plot command, assignment command, and for command. Figure 16.28 shows the Sling assemblers.

Figure 16.28. Assemblers for Sling. There is one assembler for each command, and one for each basic element of Sling. The FunctionAssembler greatly reduces the number of assemblers because it can assemble any kind of function.


Figure 16.29 shows the placement of assemblers in a Sling parser.

Figure 16.29. Sling assembler placement. This table shows the assembler that each Sling subparser employs.


16.11.1 AssignmentAssembler

This assembler pops a function and a variable, constructs an AssignFunctionCommand from these terms, and pushes the command. For example, after parsing

 y = sin(x); 

this assembler expects the variable y and the function sin(x) to be on the stack, and it constructs an assignment command from these objects.

16.11.2 ForAssembler

This assembler builds a for command from elements on the stack, which should be a variable, a from function, a to function, a " { " token, and a series of commands. This class uses the curly brace as a fence, popping commands above it and creating a composite from these commands. This composite command is the body of the for loop. This assembler pops the from and to functions and the variable, constructs a ForCommand object, and pushes it.

16.11.3 FunctionAssembler

This assembler uses information from a prototypical function object received in a constructor to guide the construction of a new function.

The constructor for this class requires a SlingFunction object that serves as a prototype. The prototype specifies the type of function to assemble and how many source functions this type needs. When this assembler works on an assembly, it pops as many functions to use as sources as the number of sources in the prototype. This assembler then creates a copy of the prototype, uses the popped sources as the copy's source functions, and pushes the copy.

16.11.4 NegativeAssembler

This assembler pops the assembly and pushes a new function that multiplies this function by “1.

16.11.5 NumAssembler

This assembler pops a number token and pushes a number function. A design principle for the sjm.examples.sling package is that "everything" is a two-dimensional function of time. Thus, if the stack contains, say, a token that is only the number 3 , this assembler pushes a new Cartesian function (t, (0, 3)) .

16.11.6 PiAssembler

This assembler pushes the function cartesian(t, (0, pi)) .

16.11.7 PlotAssembler

This class expects the target of an assembly to contain a RenderableCollection object, which is a collection of evaluatable Sling functions. This assembler pops a function, builds an AddFunctionCommand from the target and this function, and pushes the command.

16.11.8 ScaleAssembler

This class augments the stack with default scale arguments and then uses a FunctionAssembler with a Scale object to work on the assembly.

A scale requires five source functions: aFrom , a , aTo , bFrom , and bTo . These functions allow a mapping between two coordinate systems. The Sling language accepts only two arguments in a scale function: bFrom and bTo . Internally, the Sling parser defaults the "a" system to be time. For example, the Sling language function

 scale(-pi, pi) 

implies the internal scale function

 scale(0, t, 1, -pi, pi). 

The ScaleAssembler class pushes the arguments (0, t, 1) onto the stack and then uses a normal Scale object to work on the assembly.

16.11.9 SliderAssembler

This class uses one of the sliders in an assembly's target to create a Slider function and pushes this function. The class expects the assembly's target to contain a SlingTarget object, which holds references to two JSlider objects. This class's constructor determines which slider to ask for. When an object of this class works on an assembly, it gets the slider component from the target, creates a Slider function with it, and pushes this function.

16.11.10 VariableAssembler

This class expects an assembly's target to be a SlingTarget object. The target has a scope, which is a collection of variables organized by name. When this assembler works on an assembly, it pops a name from the stack, looks up a variable in the scope using the name, and pushes the variable onto the stack. This lookup creates the variable in the scope if the scope does not already contain a variable of that name .


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net