如何将基于文本的编程代码(存储在DB中)转换为实际的编程代码
本文关键字:编程 代码 DB 转换 存储 文本 于文本 | 更新日期: 2023-09-27 18:27:50
好的,我需要在DB中存储一些简单的编程代码文本,&那么我的应用程序将读取这些文本&将它们转换为实际的编程代码。
一个很好的例子是sqlfiddle.com。这个网站允许你把sql代码放进它的网站,然后点击一个按钮&u可以看到该sql代码的表结果。当然,sqlfiddle必须将您输入的基于Txt的代码存储到其数据库中。
另一个例子是w3schools.com。它允许用户提交html代码&那么用户就可以即时看到结果。例如:http://www.w3schools.com/js/tryit.asp?filename=tryjs_events
在我的情况下,我只需要使用一些有限的关键字,例如(if,for,while,+,*,-,/…)
例如,我有一个名为OrderItem的表,它有3列:
项目ID-质量-成本项目1-10-5美元第2项-12-2美元项目3-4-1美元
我还有一张编程代码表
ItemID-代码第1项-"如果数量>2,则总成本=10*5美元,否则总成本=10*4美元;返回总成本"第2项-"如果时间=上午9点,则总成本=12*2美元,否则总成本=12*1USD;返回总成本"
注意:由于现实世界中有各种各样的计算规则,代码应该能够描述真实世界,所以如果或循环&应使用所有算术运算符。
这是我的伪代码函数,但我认为它不起作用。
public String covertCode(String textBasedCode){
String[] eachWord=textBasedCode.split(" ");
if(eachWord[0].equals("if")){
//do a lot of checking. how to let the program to know this?
if(eachWord[2]>2{
return 10*5;
}
else{
return 10*4;
}
}
}
我在这个例子中使用了java,但你可以使用C++、C#php或任何你想要的编程语言。我只想知道其中的逻辑。
那么,如何将基于文本的编程代码(存储在DB中)转换为实际的编程代码呢?
您可以创建自己的迷你解释器来解释代码。它应该不会花很长时间,因为你希望它是有限的。
但由于它所做的只是计算值,我认为你可以让它们存储数字,然后按照规则在代码中进行计算。大多数数据库都内置了大量函数,所以这也可以在数据库中完成。
我只是不明白"基于文本的编程代码"与"实际编程代码"有何不同。
因此,您可以使用解释器设计模式进行此操作。可能还有其他方法,但这是我最好的猜测。如果这是你想要的,你就不会得到代码。
您可以使用Microsoft.CSharp.CSharpCodeProvider
动态编译代码。特别是,检查CompileAssemblyFromFile
查看Roslyn项目。它让您有机会将编译器作为一种服务来创建插件、代码分析器等。
http://msdn.microsoft.com/en-us/vstudio/hh500769.aspx
http://blog.filipekberg.se/2013/02/07/compilation-as-a-service-and-the-next-generation-plugins/
困难的方法是在数据库中解释代码,更简单的方法是存储c#代码并在运行时使用CodeDOM进行编译http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx
或者看看铁蟒。http://ironpython.net/
我想我在Touch建议我的解释器模式的基础上找到了一个优雅的解决方案。我以前不知道解释器模式,但读了15分钟后,我可以找到一个很酷的解决方案了。我以前从未获得过公认的答案,所以我希望我的答案能获得它。
逻辑很简单。我们必须使用"反向波兰符号表达式",如:
a b+a b c+-a b+c a--
&这就完成了。http://en.wikipedia.org/wiki/Interpreter_pattern
interface Expression {
public int interpret(Map<String,Expression> variables);
}
class Number implements Expression {
private int number;
public Number(int number) { this.number = number; }
public int interpret(Map<String,Expression> variables) { return number; }
}
class Plus implements Expression {
Expression leftOperand;
Expression rightOperand;
public Plus(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return leftOperand.interpret(variables) + rightOperand.interpret(variables);
}
}
class Minus implements Expression {
Expression leftOperand;
Expression rightOperand;
public Minus(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return leftOperand.interpret(variables) - rightOperand.interpret(variables);
}
}
class Times implements Expression {
Expression leftOperand;
Expression rightOperand;
public Times(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return leftOperand.interpret(variables) * rightOperand.interpret(variables);
}
}
class Division implements Expression {
Expression leftOperand;
Expression rightOperand;
public Division(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return leftOperand.interpret(variables) / rightOperand.interpret(variables);
}
}
class IfThen implements Expression {
Expression leftOperand;
Expression rightOperand;
public IfThen(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return (leftOperand.interpret(variables)==1) ? rightOperand.interpret(variables) : 0;
}
}
class GreaterThan implements Expression {
Expression leftOperand;
Expression rightOperand;
public GreaterThan(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return (leftOperand.interpret(variables) > rightOperand.interpret(variables)) ? 1 : 0;
}
}
class GreaterThanOrEqual implements Expression {
Expression leftOperand;
Expression rightOperand;
public GreaterThanOrEqual(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return (leftOperand.interpret(variables) >= rightOperand.interpret(variables)) ? 1 : 0;
}
}
class LessThan implements Expression {
Expression leftOperand;
Expression rightOperand;
public LessThan(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return (leftOperand.interpret(variables) < rightOperand.interpret(variables)) ? 1 : 0;
}
}
class LessThanOrEqual implements Expression {
Expression leftOperand;
Expression rightOperand;
public LessThanOrEqual(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return (leftOperand.interpret(variables) <= rightOperand.interpret(variables)) ? 1 : 0;
}
}
class Equal implements Expression {
Expression leftOperand;
Expression rightOperand;
public Equal(Expression left, Expression right) {
leftOperand = left;
rightOperand = right;
}
public int interpret(Map<String,Expression> variables) {
return (leftOperand.interpret(variables) == rightOperand.interpret(variables)) ? 1 : 0;
}
}
class Variable implements Expression {
private String name;
public Variable(String name) { this.name = name; }
public int interpret(Map<String,Expression> variables) {
if(null==variables.get(name)) return 0; //Either return new Number(0).
return variables.get(name).interpret(variables);
}
}
class Evaluator implements Expression {
private Expression syntaxTree;
public Evaluator(String expression) {
Stack<Expression> expressionStack = new Stack<Expression>();
for (String token : expression.split(" ")) {
if (token.equals("+")) {
Expression subExpression = new Plus(expressionStack.pop(), expressionStack.pop());
expressionStack.push( subExpression );
}
else if (token.equals("-")) {
// it's necessary remove first the right operand from the stack
Expression right = expressionStack.pop();
// ..and after the left one
Expression left = expressionStack.pop();
Expression subExpression = new Minus(left, right);
expressionStack.push( subExpression );
}
else if (token.equals("*")) {
Expression subExpression = new Times(expressionStack.pop(), expressionStack.pop());
expressionStack.push( subExpression );
}
else if (token.equals("/")) {
// it's necessary remove first the right operand from the stack
Expression right = expressionStack.pop();
// ..and after the left one
Expression left = expressionStack.pop();
Expression subExpression = new Division(left, right);
expressionStack.push( subExpression );
}
else if (token.equals("if-then")) {
// it's necessary remove first the right operand from the stack
Expression right = expressionStack.pop();
// ..and after the left one
Expression left = expressionStack.pop();
Expression subExpression = new IfThen(left, right);
expressionStack.push( subExpression );
}
else if (token.equals(">")) {
// it's necessary remove first the right operand from the stack
Expression right = expressionStack.pop();
// ..and after the left one
Expression left = expressionStack.pop();
Expression subExpression = new GreaterThan(left, right);
expressionStack.push( subExpression );
}
else if (token.equals(">=")) {
// it's necessary remove first the right operand from the stack
Expression right = expressionStack.pop();
// ..and after the left one
Expression left = expressionStack.pop();
Expression subExpression = new GreaterThanOrEqual(left, right);
expressionStack.push( subExpression );
}
else if (token.equals("<")) {
// it's necessary remove first the right operand from the stack
Expression right = expressionStack.pop();
// ..and after the left one
Expression left = expressionStack.pop();
Expression subExpression = new LessThan(left, right);
expressionStack.push( subExpression );
}
else if (token.equals("<=")) {
// it's necessary remove first the right operand from the stack
Expression right = expressionStack.pop();
// ..and after the left one
Expression left = expressionStack.pop();
Expression subExpression = new LessThanOrEqual(left, right);
expressionStack.push( subExpression );
}
else if (token.equals("==")) {
// it's necessary remove first the right operand from the stack
Expression right = expressionStack.pop();
// ..and after the left one
Expression left = expressionStack.pop();
Expression subExpression = new Equal(left, right);
expressionStack.push( subExpression );
}
else
expressionStack.push( new Variable(token) );
}
syntaxTree = expressionStack.pop();
}
public int interpret(Map<String,Expression> context) {
return syntaxTree.interpret(context);
}
}
&就是这样。要调用conversing方法,我们只需要这个。看到这个简单的代码:
String expression = "w x == z if-then";
//String expression = "w x - z +";
Evaluator sentence = new Evaluator(expression);
Map<String,Expression> variables = new HashMap<String,Expression>();
variables.put("w", new Number(10));
variables.put("x", new Number(5));
variables.put("z", new Number(42));
int result = sentence.interpret(variables);
System.out.println(result);
&完成了。但它有一个限制,它没有一个循环,但若那个对我来说已经足够了
我做得对吗?