Skip to content

Expressions Blocks Basics

Johan Hernandez edited this page Nov 23, 2011 · 4 revisions

All the expressions begins with the 'at' symbol @ in the key or the name of the JSON property:

Example:

{
    "@return":50
}

Result

50

If the key doesn't have an @ symbol then is not a fire expression, it's a regular JSON value.

Example:

{
    "return":50
}

Result

{
    "return":50
}

Expressions Syntax

Any expression has at least 3 parts.

  • Key: Name of the expression.
  • hint: (Optional) A string that can be used by in different ways by the called expression.
  • Input: (Optional) a JSON value used as arguments of the expression.

In the following example set is the name of the expression, variable1 is the hint and 2 is the input.

{
    "@set(variable1)": 2
}

The syntax would be like this:

{
    "@<expression-name>(<hint>)": <input>
}

When all the keys of an object definition begins with @ then the whole object definitions become to be an expression-block. Example, the following json object is interpreted as a literal JSON value becase it contains both expressions and regular JSON keys at the same level of the document:

{
    "@set(name)": "Name",
    "regularJSONNumber": 2000
}

The previous code should be formatted as follows:

{
    "@set(name)": "Name",
    "@return":{
        "regularJSONNumber": 2000
    }
}

And the result would be:

{
    "regularJSONNumber": 2000	
}

All the expressions are evaluated as a series, one after the other, the result of the last expression in the block is the result of the whole expression block:

Example, this block contains two expression calls, even when both are executed the result of the last expression becomes the final result of the block:

{
    "@return": 2,
    " @return": 5,
}

The result is:

5

Dealing with JSON Keys with the same name

JSON keys must be unique at the same level, if there is a repeated key the parser will ignore it.

Example, the following fire.js is a valid JSON Document but only one @set expression will reach the fire.js compiler because the parser will ignore the second @set expression as the key is the same for the two entries:

{
    "@set(x)": 23,
    "@set(x)": 25
    "@return": {
        "@get(x)": null
    }
}

The result will be usually 23. We say 'usually' because since this is an error of the JSON Document and parser is free to take any decision to continue parsing. To solve this situation the fire.js compiler will ignore any whitespace before the @ symbol, so for JSON you actually have different keys but for the compiler is a different expression call with the same expression name and hint:

Example:

{
	"@set(x)": 23,
	" @set(x)": 25
    "@return": {
		"@get(x)": null
	}
}

You can use as many white-spaces characters as you need.

Note: Dealing with duplicated expression keys or missing expression calls is unlikely to happen if you use Fire.js IDE which will automatically detect duplicated keys and add the necessary white-space characters before saving your JSON source code. Check other advantages of using Fire.JS IDE.

Input Processing

Since the input can be any JSON value it can also become another fire.js expression.

In the following example you can see how the input of the high level @return is actually the result of nested @return call, Example:

{
    "@return": {
    	"@return": {
			"point": {
				"x": 22.4,
				"y": 45.8
			}
		}
	}
}

The result:

{
   "point": {
		"x": 22.4,
		"y": 45.8
	}
}

Depending of how the expression works the input can be ignored or processed. Conditional expressions like @if and @else uses this technique.

Example:

{
	"@set(chuckNorrisIsHere)": false,
	"@if(chuckNorrisIsHere)": "Chuck is here, You are dead!",
	"@else": "It's never here!, you are safe!"
}

The result is:

"It's never here!, you are safe!"

Thanks for Reading. Please take your time to read other tutorials.

Contributors

If you find any error in this tutorial(misspelling errors, syntax errors, etc) please send me an email with your corrections, I'll be more than happy to add you in this list.