Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » A problem with ==, ||, and other(I try reuse XExpression)
A problem with ==, ||, and other [message #1059458] Sat, 18 May 2013 22:33 Go to next message
Yoandri Saavedra is currently offline Yoandri SaavedraFriend
Messages: 24
Registered: February 2013
Junior Member

My .text file is..
generate go "http://www.google.com/org/Go"

Model returns xbase::XExpression:
	{Model}
	(expressions+=XVariableDeclaration | expressions+=XExpression )*;
	
XPrimaryExpression returns xbase::XExpression:
	XConstructorCall |
	XBlockExpression |
	XSwitchExpression |
	XFeatureCall |
	XLiteral |
	
	XForLoopExpression |
	XWhileExpression |
	XDoWhileExpression |
	XThrowExpression |
	XReturnExpression |
	XTryCatchFinallyExpression |
	XParenthesizedExpression |
	
	XIfExpressionMy ||

	; 

XIfExpressionMy:
	'yeah' '(' if=XExpression ')'
	then+=XExpression
	(=>'oNo' else+=XExpression)?;


I try reuse XExpression.. but when I write
  yeah (true == true)
       var int y = 3
  oNo 
      var int x = 9

say error in ==, extraneous input
Re: A problem with ==, ||, and other [message #1059488 is a reply to message #1059458] Sun, 19 May 2013 17:13 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2013-18-05 15:33, Yoandri Saavedra wrote:
> XIfExpressionMy ||

What are the two pipes doing there?
Do you want to recognize "nothing" as a PrimaryExpression ?

- henrik
Re: A problem with ==, ||, and other [message #1059624 is a reply to message #1059488] Mon, 20 May 2013 21:13 Go to previous messageGo to next message
Yoandri Saavedra is currently offline Yoandri SaavedraFriend
Messages: 24
Registered: February 2013
Junior Member

I only use what I saw in the Xbase.text ... I want to do in Spanish language and interpret ... just change for example where it says "if" with "if" ... but I'm doing it little by little with what I find ... there any way to reuse Xbase for what I want??? ...
Re: A problem with ==, ||, and other [message #1059625 is a reply to message #1059624] Mon, 20 May 2013 21:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi,

i acutally cannot reproduce this. there must be something else in your case.
can you share a complete reproduceable grammar.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: A problem with ==, ||, and other [message #1059630 is a reply to message #1059625] Mon, 20 May 2013 23:20 Go to previous messageGo to next message
Yoandri Saavedra is currently offline Yoandri SaavedraFriend
Messages: 24
Registered: February 2013
Junior Member

Thaks, very... my .xtext
grammar cu.lt.vlenin.JE with org.eclipse.xtext.xbase.Xbase

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase

generate jE "http://www.lt.cu/vlenin/JE"

Model returns xbase::XExpression:
	{Model}
	(expressions+=XVariableDeclaration | expressions+=XExpression )*;
	
XPrimaryExpression returns xbase::XExpression:
	XConstructorCall |
	XBlockExpression |
	XSwitchExpression |
	XFeatureCall |
	XLiteral |
	
	XForLoopExpression |
	XWhileExpression |
	XDoWhileExpression |
	XThrowExpression |
	XReturnExpression |
	XTryCatchFinallyExpression |
	XParenthesizedExpression |
	
	XIfExpressionMy |
	JImprimeSalida |
	JAsignacion |
	JFuncion |
	JDeclaraVariable |
	XWhileExpressionMy |
	JleerEntrada

	; 

XWhileExpressionMy returns XExpression:
	{XWhileExpressionMy}
	'mientras' '(' predicate=XExpression ')'
		body=XExpression;



JDeclaraVariable:
	'crear' (tipo='entero'|'cadena') name=ValidID  ('=' right=JMultivalor)? ';'
;

JFuncion:
	'función' name=ValidID  "(" 
		(params+=Expression  ("," params+=Expression)* )?  ")"
	 body+=XExpression
;

XIfExpressionMy:
	'si' '(' if=XExpression ')'
	then+=XExpression
	(=>'sino' else+=XExpression)?;
	
JMultivalor:
	STRING |INT 
;

JImprimeSalida:
		"escribir"  (name= ValidID | contenido=JMultivalor) ';'
;
JleerEntrada:
	{JleerEntrada}
	name="entrar" ';'
;

JAsignacion:
	'asignar' name=ValidID '=' der=Expression ";"
;

Expression returns Expression:
	Comparison
;

Comparison returns Expression:
	Addition ({Equals.left=current}"==" right=Addition)?;

	
Addition returns Expression:
	Supstracion ({Plus.left=current}"+" right=Supstracion)*;

Supstracion returns Expression:
	Multiplication ({Menus.left=current}"-" right=Multiplication)*;
	
Multiplication returns Expression:
	Dividation ( {Multi.left=current} "*" right=Dividation)*; 

Dividation returns Expression:
	PostfixOperators ( {Divid.left=current} "/" right=PostfixOperators)*; 
	
PostfixOperators returns Expression:
	JAtomic ({ArrayAccess.expr=current} "[" index=Expression "]")?;

JAtomic returns Expression:
	{SymbolRef} symbol=ValidID ("(" (actuals+=Expression)? ("," actuals+=Expression)* ")")? |	// FUNC
	{NumberLiteral} value=INT | 
	{StringLiteral} value=STRING;


And my xbase is interpreter..


@SuppressWarnings("restriction")
public class JEInterprete extends XbaseInterpreter{

	@Inject
	private Primitives primitives;
	
	
	
	
	protected Object _evaluateVariableDeclaration(XVariableDeclaration variableDecl, IEvaluationContext context, CancelIndicator indicator) {
		super._evaluateVariableDeclaration(variableDecl, context, indicator);
		return context.getValue(QualifiedName.create(variableDecl.getName()));
	}
	
	
	protected Object _evaluateJImprimeSalida(JImprimeSalida is, IEvaluationContext context, CancelIndicator indicator) {
		
		if (is.getName() == null) JEConsola.Escribir(is.getContenido().toString()); 
		else {
			 Object v = context.getValue(QualifiedName.create(is.getName()));
			 if (v == null) JEConsola.Escribir("Variable no declarada: "+is.getName());
			 else 
				 JEConsola.Escribir(v.toString());			
		}
		
	
		return null ;
		
		
	}
	protected Object _evaluateJleerEntrada(JleerEntrada jl, IEvaluationContext context, CancelIndicator indicator) {
		
			return JEConsola.Leer() ;
			
			
		}
	

	protected Object _evaluateIfExpressionMy(XIfExpressionMy ifExpression, IEvaluationContext context, CancelIndicator indicator) {
		
		
		Object conditionResult = internalEvaluate(ifExpression.getIf(), context, indicator);
		if (Boolean.TRUE.equals(conditionResult)) {
		
			List<XExpression> expressions = (List<XExpression>) ifExpression.getThen();
			Object result = null;
			for (int i = 0; i < expressions.size(); i++) {
				result = internalEvaluate(expressions.get(i), context, indicator);
			}
			return result;
		} else {
			if (ifExpression.getElse() == null)
				return null;
			List<XExpression> expressions = (List<XExpression>) ifExpression.getElse();
			Object result = null;
			
			for (int i = 0; i < expressions.size(); i++) {
				result = internalEvaluate(expressions.get(i), context, indicator);
			}
			return result;
			
		}
	}
	
	
	protected Object _evaluateJFuncion(JFuncion fun, IEvaluationContext context, CancelIndicator indicator) {
		List<XExpression> expressions = (List<XExpression>) fun.getBody();

		Object result = null;
		IEvaluationContext forkedContext = context.fork();
		for (int i = 0; i < expressions.size(); i++) {
			result = evaluate(expressions.get(i), forkedContext, indicator);
		}
		return result;
		
	}
	protected Object _evaluateJDeclaraVariable(JDeclaraVariable dv, IEvaluationContext context, CancelIndicator indicator){
		
	
		Object initialValue = null;

		if (dv.getRight()!=null) {
			initialValue = dv.getRight();

		} else {
			if (dv.getTipo() != null) {
				switch(dv.getTipo()) {

				case "cadena":  initialValue = ""; break;
				case "entero":  initialValue = Integer.valueOf(0); break;
					default:
						throw new IllegalStateException("Unknown primitive type " + dv.getTipo().toString());
				}
			}
		}
		
		context.newValue(QualifiedName.create(dv.getName()), initialValue);
		return null;
	}
	protected Object _evaluateJAsignacion(JAsignacion dv, IEvaluationContext context, CancelIndicator indicator){
		
		
		Object result = _evaluateExpression(dv.getDer(),  context,   indicator);
		
		if (result == null){
			JEConsola.Escribir("Error."+dv.getName()+ " No pertenece a una clase válida");
			return null;
			}
		context.assignValue(QualifiedName.create(dv.getName()), result);	
		return context.getValue(QualifiedName.create(dv.getName()));
		
	}


	protected Object _evaluateNumberLiteral(NumberLiteral nl, IEvaluationContext context,  CancelIndicator indicator){
		
		return nl.getValue();
	}	
	protected Object _evaluateStringLiteral(StringLiteral sl, IEvaluationContext context, CancelIndicator indicator){
		
		return sl.getValue();
	}	
	protected Object _evaluateXWhileExpressionMy(XWhileExpressionMy sl, IEvaluationContext context, CancelIndicator indicator){
		/// ???
		return null;
	}
	protected Object _evaluateExpression(Expression ad, IEvaluationContext context, CancelIndicator indicator){
		
		Object result = null;
		
		if (ad instanceof NumberLiteral)
			result = _evaluateNumberLiteral((NumberLiteral) ad, context, indicator);
		else if (ad instanceof StringLiteral)
			result = _evaluateStringLiteral((StringLiteral) ad, context, indicator);
		else if (ad instanceof Plus)
			result = _evaluatePlus((Plus)ad, context, indicator);
		else if (ad instanceof Multi)
			result = _evaluateMulti((Multi)ad, context, indicator);
		else if (ad instanceof Menus)
			result = _evaluateMenus((Menus)ad, context, indicator);
		else if (ad instanceof Divid)
			result = _evaluateDivid((Divid)ad, context, indicator);
		else if (ad instanceof SymbolRef)
			result = _evaluateSymbolRef((SymbolRef)ad, context,  indicator);
		
		return result;
	}


	private Object _evaluateMulti(Multi ad, IEvaluationContext context, CancelIndicator indicator) {
		return ((int) _evaluateExpression(ad.getLeft(), context, indicator) * 
				(int) _evaluateExpression(ad.getRight(), context, indicator)) ;
	}


	private Object _evaluatePlus(Plus ad, IEvaluationContext context, CancelIndicator indicator) {
		return ((int) _evaluateExpression(ad.getLeft(), context, indicator) + 
				(int) _evaluateExpression(ad.getRight(), context, indicator)) ;
	} 
	
	private Object _evaluateMenus(Menus ad, IEvaluationContext context, CancelIndicator indicator) {

		return ((int) _evaluateExpression(ad.getLeft(), context, indicator) - 
				(int) _evaluateExpression(ad.getRight(), context, indicator)) ;
	} 
	private Object _evaluateDivid(Divid ad, IEvaluationContext context, CancelIndicator indicator) {
		return ((int) _evaluateExpression(ad.getLeft(), context, indicator) / 
				(int) _evaluateExpression(ad.getRight(), context, indicator)) ;
	} 
	
	private Object _evaluateSymbolRef(SymbolRef ad, IEvaluationContext context, CancelIndicator indicator) {
		
		if ((int)context.getValue(QualifiedName.create(ad.getSymbol())) == 0)
			System.out.println("Es cero");
		
		
		return context.getValue(QualifiedName.create(ad.getSymbol()));


	}
	
}
Re: A problem with ==, ||, and other [message #1059664 is a reply to message #1059630] Tue, 21 May 2013 07:02 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hmmm

why are you bypassing xbase so much. and then: ig needed why use it at all.
never the less the editor still works fine with your example model.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Setting up URI imports
Next Topic:Reading a model
Goto Forum:
  


Current Time: Sat Apr 27 03:05:00 GMT 2024

Powered by FUDForum. Page generated in 0.02890 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top