Some times labels is used before declared, for example:
void test() {
goto label;
label: return;
}
when an one pass compiler parses the first statement, it doesn't know where the label is, until the label: statement comes.
Since one pass compiler only parses the code once, there's no way to leave the label alone and comes back later, right?
So what is the usual way to deal with this in an one compiler?
Two possibilities:
Backpatch. Use the destination address field in the generated branch operation to create a linked list of unresolved uses of the label, putting the head of the list in the label symbol table. When the label is defined, walk the list, overwriting ("patching") each link with the correct value.
If you're allowed to generate symbolic assembly code, just output the label name and let the assembler deal with it.
Related
I have an Objective C++ program used to handle the setup of our different applications. Is there a way to use preprocessor defines to create text to be substituted in the strings used by NSTextFieldCell, NSButtonCell?
FOR EXAMPLE, instead of have an NSTextField that says "Options for setting up Foo", there would be a preprocessor macro (GCC_PREPROCESSOR_DEFINITIONS):
MY_PROGRAM_NAME=Bar
and then the text for NSTextField would be:
"Options for setting up $(MY_PROGRAM_NAME)"
Which would then have the desired result: "Options for setting up Bar"
NOTE 1: obviously, I could do the substitution programmatically in code.
Note 2: this is for Xcode 7, so perhaps there isn't a feature like this?
In a word, no. The Xcode nib compiler doesn't perform any kind of variable substitution and—once encoded—all archived property values are static.
However, if this is a "thing" for you application, and there aren't too many view classes involved (say, just NSTextField), it wouldn't be hard to roll your own solution.
I'd consider this approach:
Concoct a simple-to-substitute syntax, a la "Some string {VAR_NAME}".
Define your variables as key/value pairs in a dictionary. Store the
dictionary as an XML file / dictionary in the app bundle.
At app startup, load the dictionary and make it public by putting it in
a global variable or adding it to -[NSUserDefaults registerDefaults:]
Subclass NSTextField (as an example). Override either
-initWithCoder: or -awakeFromNib. In the override, get the string
value of the view object, scan it for substitutions using the public
variable dictionary, and update the string property as appropriate.
In IB, change the class of any NSTextField that needs this feature to
your custom subclass.
Another possible approach would be to have multiple targets in your project and a separate Localizable.strings file for each of these. This of course assumes, that you use Localizable.strings, even if you may support only one language.
i am currently doing an app that has language change feature. However,for every string that i wish to have changes made to when a different language is selected, i have to implement the following code
"Hello world".localize()
However, as the app gets bigger, the code become very messy in the way that all the strings in their respective view controllers have this .localize() append to it.
Is there a way where i can do this .localize() thing in one central place?
EDIT: I tried to create a Strings.swift file and put all the strings inside. I did something like this.
static let relevantString = "hello world".localize()
and then in the view controllers i call
let myString = relevantString
However, this does not well. The strings will only change after i terminate the app and restart it.
Your attempt to use static let fails to produce the dynamic behaviour you want simply because you used a constant. You could use a read only computed property instead, something like:
var relevantString : String { return "Hello World".localize() }
As an alternative, as you seem to be more concerned over the clutter, you could define a simple prefix or postfix unary operator which called localize() on its argument. Swift allows a number of symbols to be used as operators so the operator can just be a single character. For example you could define, say, § so that §"Hello World" produced a localised string.
HTH
I am using the Rascal library for accessing syntax trees that are produced by the Eclipse Java compiler (JDT.rsc).
I am trying to get a fix on how the abstract syntax tree works. One thing that eludes me is the "variableBinding". Imagine a very simple class MyClass with one method doNothing() containing one statement, a variable declaration myVar. The declaration of the string variable myVar is represented in the AST-snipped below.
Inside the #bindings annotation, under the variableBinding key, there is a list that represents the consecutive components of a path to the variable myVar. The last item represents the actual variable itself, which is represented by the Id constructor variable(str name, int id).
Question: What is the meaning of the id?
It certainly isn't unique, because when I duplicate the method doNothing() and name it doNothing2(), I find variable("doNothing",0) and variable("doNothing2",0) in the AST. What exactly does it identify?
...
variableDeclarationFragment(
"myVar",
none())[
#bindings=(
"typeBinding":entity([
package("java"),
package("lang"),
class("String")
]),
"variableBinding":entity([
class("MyClass"),
method(
"doNothing",
[],
entity([primitive(void())])),
variable("myVar",0) // Here it's 0, but
])
),
#javaType=entity([
package("java"),
package("lang"),
class("String")
]),
#location=|project://my-project/src/MyClass.java|(60,5,<4,15>,<4,19>)
]
...
This id field actually appears to be an identifier assigned internally by Eclipse. You can see the JavaDoc for the IVariableBinding interface here, which is the source of the id:
http://help.eclipse.org/indigo/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/IVariableBinding.html#getVariableId()
Just click on the link for getVariableId() if it doesn't take you there, and instead just takes you to the page for the interface. This is the method called to get the id. From what I can tell, it isn't very useful and can probably safely be ignored in most cases.
I have a working grammar on xtext, and am starting the validation of the code.
For this, I added a method in the validator xtext created for me.
Of course, when an expression isn't valid, I want to be able to give a warning on the given AST node.
I attempted the obvious:
#Check
public void testCheck(Expression_Multiplication m){
if(!(m.getLeft() instanceof Expression_Number)){
warning("Multiplication should be on numbers.",m.getLeft());
}
if(!(m.getRight() instanceof Expression_Number)){
warning("Multiplication should be on numbers.",m.getRight());
}
}
Without success, as Expression_Number extends EObject, but is not an EStructuralFeature.
warning(String message, EStructuralFeature feature)
There are many other prototypes for warning, but none that takes just a String and a Eobject. Using null or various values extracted from eContainingFeature logs an error, and sometimes shows the warning at the correct place anyway. Searching for examples, I found that the values were often coming from the statics fields of a class called Literals or ***Package, the one generated in the project contains EStructuralFeatures, but I have no idea of which one to use, or why I would need one of these.
So the question is:
How can I place a warning on a given AST element ?
The EStructuralFeature is the property of your AST. You'll find a generated EPackage class, which contains constants.
I guess in your case it is something like:
MyDslPackage.Literals.EXPRESSION_MULTIPLICATION__LEFT
and
MyDslPackage.Literals.EXPRESSION_MULTIPLICATION__RIGHT
I ended up using
private void warning(String text, EObject badAstNode){
// The -1 seems to come from a static member somewhere. Probably cleaner to
// name it, but I couldn't find it again.
warning(text,badAstNode,null,-1);
}
I have no idea about whether this is supposed to be the right way, but it seemed to work in the various cases I used it, and requires a minimal amount of state to be kept.
I have a JavaFX GUI in an fxml file with its controller class defined. I have two text items that I want in that GUI, one tied to a variable whose value does not change until the user reloads the screen, the other I would think needs to be a StringProperty as it shows the running total of a column in my TableView. Because of what they are, I'm trying to use Label instead of a TextField as their display control.
I liked Sebastian's answer to this problem here:
Displaying changing values in JavaFx Label
however, when I try to use it I get a compile error that says:
cannot find symbol
symbol: variable textProperty
location: variable salesNoLabel of type Label
I'm not sure what I'm doing wrong, but to start with, my label text is initially set in the fxml file, so in my controller I just have its fx:id substituted for "myLabel" listed in Sebastian's answer:
salesNoLabel.textProperty.bind(sn);
where salesNoLabel is the fx:id of the label and sn is a string variable.
Not sure if you need to see more of my code to help me with this problem, but thanks in advance for checking it out.
Sebastian's answer had a syntax error, I edited it to fix it.
You need to invoke the textProperty() method on the label:
salesNoLabel.textProperty().bind(sn);
Note the addition of parentheses after the textProperty identifier to invoke the method.