Execute String

Blitz3D Forums/Blitz3D Programming/Execute String

ClayPigeon(Posted 2011) [#1]
If there is already a topic on this, I couldn't seem to find it. Is there any way that I could store an expression in a string, and be able to execute it? I am using finite state machines to control the AI's in my game, and I need to store conditions in an array, so when it comes time to do checks, I can just go through each entry in the string array and evaluate the strings. Is this possible? Or is there a better way of doing what I'm trying to do? Thanks!


Warner(Posted 2011) [#2]
In that case, using a Select..Case might be the most suitable solution.
But there are several solutions for this. I searched for eval site:blitzbasic.com to find this archive entry:
http://www.blitzmax.com/codearcs/codearcs.php?code=407


ClayPigeon(Posted 2011) [#3]
I think I'll stick to Selects, because it's not worth the work to evaluate strings like that. Plus, by using a Select, I have more flexibility and control over my states.


GW(Posted 2011) [#4]
If you want to just evaluate a string expression there are many posts in the code archives about it, but all of them are broken in some way.
Here is another one i put together
Function Eval#(sExp$,lExp$="",n#=0,op$="+")
	Local m_next$
	Local m#
	Print lexp
	If Len(sExp) Then
		lExp = sExp.Replace(" ","") + ")"
	EndIf
	
	While op <> ")"
		m_next = pop(lexp)
		
		Print lexp
		
		If m_next = "(" Then 
			m_next = eval("",lexp)
		Else
			While "+-*/)".find(Chr(lexp[0])) = -1 
				m_next :+ pop(lexp)
			Wend
		EndIf
		m = Float(m_next)
		Select op
			Case "+" n=n+m
			Case "-" n=n-m
			Case "*" n=n*m
			Case "/" n=n/m'(m Or 1.0)
			Default RuntimeError("!!!")	
		EndSelect	
		op = pop(lexp)
	Wend
	Return n
End Function
Function pop$(s$ Var)
	Local tmp$ = Chr(s[0])
	s = Right(s,s.length-1)
	Return tmp
End Function
Print eval("190/-2")


It sounds more like you need a scripting language.
Do a forum search for:

BVM

MicroC

LugI





.


Yasha(Posted 2011) [#5]
It sounds like your situation might be a pretty good match for this library I posted a while back: http://www.blitzbasic.com/Community/posts.php?topic=92323

It accepts strings containing code written in the C programming language, and compiles them at runtime to machine code in memory. The result runs about as fast as compiled B3D code, so it's a lot faster than scripting engines or anything like that.

C is obviously a very different language from B3D overall, but simple expressions and function calls are going to be pretty much the same between the two (just remember that ending semicolon). You could write a wrapper function that packages your domain-specific expression language with a pre-written framework function so that you don't have to defile your game logic with anything that looks like C.

You could also do a search for scripting languages, as one of those would also do what you want. However, interpreted bytecode is slow and, to be brutally honest, none of the currently available B3D scripting libraries are very good.