this fails because the linspace should not get unfixed. we still have to be lispy with function calls (func args) Since we expand to code, we don't seem to have the namespace issues since the code is executed in our script. Now, we illustrate that the output code can be evaluated. The last case could be simplified, but it would take some logic to make sure all the operators are the same, and that handles if any of the operands are expressions. First, an example with debug we can see what happens. ( when debug ( print ( * " " indent) "true: " code)) ~(nfx ( get code 0) ( + indent 1) ( cut code 2) ( + indent 1) debug)))])] `(~(nfx ( get code 1) ( + indent 1) debug) longer expression, swap first two and take the rest. ~(nfx ( get code 2) ( + indent 1) debug))] ( when debug ( print ( * " " indent) "3: " code)) ~(nfx ( get code 1) ( + indent 1) debug))] `(~(nfx ( get code 0) ( + indent 1) debug) ( when debug ( print ( * " " indent) "2: " code)) ( when debug ( print ( * " " indent) "1: " code)) ( when debug ( print "list: " code " type: " (type code))) ( when debug ( print ( * " " indent) "code: " code " type: " (type code))) We assume that CODE is in infix notation." "Transform the CODE expression to prefix notation. The idea is to take an expression as a string, parenthesize it, and then we could eval it. Not too pretty, but correct! The wikipedia page provides an example C program to implement this, and we adapt it here for hy. add )) at the end of the expression and before each right parenthesis in the original expression.add (( at the beginning of each expression and after each left parenthesis in the original expression and.replace * and / with )*( and )/(, respectively.replace + and – with ))+(( and ))-((, respectively.Apparently in Fortran I (yep, one) the compiler would expand each operator in an expression with a sequence of parentheses to get the precedence right ( )! You can fully parenthesize your expressions for this, but it is tedious and hard to read. I am continuing some investigation in getting operator precedence right with infix notation. It only converts a division expression correctly (and only if all the arguments are numbers and not other expressions), but it illustrates that we can use a program as data, and do different things with it! The other takes the program, and outputs a LaTeX representation of it. One simply evaluates the program, so we can use it. Here we define two macros that both take a program as input. Sure we may want to run it, but maybe we would like a different representation of the program than the code too. I bet you didn't think we could use a hy program for more than one thing. In Lisp, the code is already in the abstract tree form, and we manipulate it more directly. That is not as clear as what we did in hy! Why? Because we had to transform the Python to AST, and manipulate it. ![]() # parse the statement p = ast.parse( "print 5 + 6")
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |