Commit d02f70d0 authored by Ole Voldsæter's avatar Ole Voldsæter

function calls in expressions conform to new schema

parent 6db64478
......@@ -122,19 +122,19 @@
:mod-varlist (vec (map #(if (= % (first args)) (second args) %) *variables*))})
:filter (fn [& args] (first args))
:expression (fn [& args] (first args))
:disjunction (expr-transform "or" false)
:conjunction (expr-transform "and" false)
:equality_expression (expr-transform "eq" false)
:not_equal_expression (expr-transform "ne" false)
:ge_expression (expr-transform "ge" false)
:le_expression (expr-transform "le" false)
:lt_expression (expr-transform "lt" false)
:gt_expression (expr-transform "gt" false)
:additive_expression (expr-transform "addition" false)
:subtractive_expression (expr-transform "subtraction" false)
:multiplicative_expression (expr-transform "multiplication" false)
:divisive_expression (expr-transform "division" false)
:power (expr-transform "power" false)
:disjunction (expr-transform "or")
:conjunction (expr-transform "and")
:equality_expression (expr-transform "eq")
:not_equal_expression (expr-transform "ne")
:ge_expression (expr-transform "ge")
:le_expression (expr-transform "le")
:lt_expression (expr-transform "lt")
:gt_expression (expr-transform "gt")
:additive_expression (expr-transform "addition")
:subtractive_expression (expr-transform "subtraction")
:multiplicative_expression (expr-transform "multiplication")
:divisive_expression (expr-transform "division")
:power (expr-transform "power")
:negation (fn [& args]
(if (second args)
{:function "not"
......
......@@ -4,24 +4,41 @@
} stata2sdtl.functions)
(def function-mapping
{"abs" "absolute_value"
"cos" "cos"
"exp" "exponentiation"
"log10" "logarithm_base_10"
"mod" "modulo"
"ln" "natural_logarithm"
"log" "natural_logarithm"
"runiform" "random_variable_uniform"
"round" "round"
"sin" "sine"
"sqrt" "square_root"
"missing" "missing_list"}
{"abs" "absolute_value"
"cos" "cos"
"exp" "exponentiation"
"log10" "logarithm_base_10"
"mod" "modulo"
"ln" "natural_logarithm"
"log" "natural_logarithm"
"runiform" "random_variable_uniform"
"round" "round"
"sin" "sine"
"sqrt" "square_root"
"missing" "missing_list"
"or" "or"
"and" "and"
"eq" "eq"
"ne" "ne"
"ge" "ge"
"le" "le"
"lt" "lt"
"gt" "gt"
"addition" "addition"
"subtraction" "subtraction"
"multiplication" "multiplication"
"division" "division"
"power" "power"}
)
(defn function-lookup [& args]
(let [canonical (get function-mapping (first args))]
{:function (or canonical (first args))
:arguments (rest args)
:arguments (map-indexed
(fn [i arg] {:argumentName (str "EXP" (inc i))
:argumentValue arg})
(rest args)
)
:isSdtlName (some? canonical)
"$type" "FunctionCallExpression"}
)
......
......@@ -4,6 +4,7 @@
} stata2sdtl.transform-helpers
(:require
[stata2sdtl.utils :refer :all]
[stata2sdtl.functions :refer [function-lookup]]
)
)
......@@ -26,13 +27,10 @@
)
)
(defn expr-transform [name native]
(defn expr-transform [name]
(fn [& args]
(if (second args)
{:function name
:arguments args
:isSdtlName (not native)
"$type" "FunctionCallExpression"}
(apply function-lookup name args)
(first args)
)
)
......
......@@ -111,51 +111,65 @@
)
(is
(= (parse-and-transform "generate a=!(2^-5)/5*3+7-x < 5 | y if 1")
{:command "compute"
:condition {"$type" "NumericConstantExpression"
:value 1}
:expression {"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "NumericConstantExpression"
:value 2}
{"$type" "NumericConstantExpression"
:value -5}]
:function "power"
:isSdtlName true}]
:function "not"
:isSdtlName true}
{"$type" "NumericConstantExpression"
:value 5}]
:function "division"
:isSdtlName true}
{"$type" "NumericConstantExpression"
:value 3}]
:function "multiplication"
:isSdtlName true}
{"$type" "FunctionCallExpression"
:arguments [{"$type" "NumericConstantExpression"
:value 7}
{"$type" "VariableSymbolExpression"
:variableName "x"}]
:function "subtraction"
:isSdtlName true}]
:function "addition"
:isSdtlName true}
{"$type" "NumericConstantExpression"
:value 5}]
:function "lt"
:isSdtlName true}
{"$type" "VariableSymbolExpression"
:variableName "y"}]
:function "or"
:isSdtlName true}
:mod-varlist ["a"]
:variables ["a"]})
'{:command "compute"
:condition {"$type" "NumericConstantExpression"
:value 1}
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "NumericConstantExpression"
:value 2}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value -5}})
:function "power"
:isSdtlName true})
:function "not"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 5}})
:function "division"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 3}})
:function "multiplication"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "NumericConstantExpression"
:value 7}}
{:argumentName "EXP2"
:argumentValue {"$type" "VariableSymbolExpression"
:variableName "x"}})
:function "subtraction"
:isSdtlName true}})
:function "addition"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 5}})
:function "lt"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "VariableSymbolExpression"
:variableName "y"}})
:function "or"
:isSdtlName true}
:mod-varlist ["a"]
:variables ["a"]})
"Should be a complex compute with condition"
)
(is
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment