如何将基于文本的编程代码(存储在DB中)转换为实际的编程代码?

时间:2013-08-15 08:07:14

标签: c# java php c++ programming-languages

好的,我需要在DB中存储一些简单的编程代码文本,&然后我的应用程序将阅读这些文本&将它们转换为实际的编程代码。

一个很好的例子是sqlfiddle.com。这个网站允许你把sql代码放入它的网站,你然后点击一个按钮&你可以看到那个sql代码的表结果。 Ofcourse sqlfiddle必须存储您输入其数据库的基于Txt的代码。

另一个例子是w3schools.com。它允许用户提交HTML代码和然后用户可以即时查看结果。例如:http://www.w3schools.com/js/tryit.asp?filename=tryjs_events

在我的情况下,我只需要使用一些有限的关键字,如(if,for,while,+,*, - ,/ ...。)

例如,我有一个名为OrderItem的表,其中包含3列:

ItemID - Qantity -Cost
Item1 - 10 - 5 USD
Item2 - 12 - 2 USD
Item3 - 4 - 1 USD

我还有一个表ProgrammingCode

ItemID - Code
Item1 - "if quantity > 2 then totalcost=10*5 USD else totalCost=10*4USD; return totalCost"
Item2 - "if time=9am then totalcost=12*2 USD else totalCost=12*1USD; return totalCost "

注意:由于现实世界中存在各种各样的计算规则,因此代码应该能够描绘真实世界,因此如果或循环和放大应该使用所有算术运算符。

这是我的Pseudocodefunction,但我觉得它不起作用。

    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中)转换为实际的编程代码?

5 个答案:

答案 0 :(得分:2)

您可以创建自己的迷你解释器来解释代码。它不应该花费很长时间,因为你希望它受到限制。

但是,由于它只是计算值,我认为你可以让它们存储数字,然后按照规则在代码中进行数学运算。大多数数据库都内置了大量功能,因此也可以在数据库中完成。

我只是看不出“基于文本的编程代码”与“实际编程代码”有什么不同。

因此,您可以使用解释器设计模式。可能还有其他方法,但这是我最好的猜测。如果那就是你要找的东西,你就不会得到代码。

答案 1 :(得分:1)

您可以使用Microsoft.CSharp.CSharpCodeProvider即时编译代码。特别是,请检查CompileAssemblyFromFile

答案 2 :(得分:1)

查看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/

答案 3 :(得分:0)

困难的方法是解释数据库中的代码,更简单的方法是存储c#代码并运行时使用CodeDOM编译它http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx

或者看看Iron Python。 http://ironpython.net/

答案 4 :(得分:0)

我想我找到了一个优雅的解决方案,基于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);
    }
}

&安培;而已。要调用转换方法,我们只需要这个。看到这个简单的代码:

    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);

&安培;完成了。但它有限制,它没有循环,但如果那时对我来说足够了。

我做得对吗?