Commit a689d40b authored by Sigbjørn Revheim's avatar Sigbjørn Revheim

More fixes

parent eb9c9b15
......@@ -33,11 +33,6 @@
:NumberRangeEnd to
:NumberRangeIncrement increment})
(defn variable-symbol-expression
[varname]
{"$type" "VariableSymbolExpression"
:VariableName varname})
;; mapping of grammar production names to transformation functions
(def transformations
{
......@@ -49,10 +44,10 @@
:message (str "Ignoring \"" (-> args first trim) "\"")})
:invalid (fn [& args] {"$type" "Invalid"
:Command "invalid"
:message (str "Invalid/unexpected syntax for \"" (-> args first trim) "\"")
:messages [{:messageText (str "Invalid syntax for " (first args))
:severity "error"}]})
:unsupported (fn [& args] {"$type" "Unsupported"
:Message (str "Invalid/unexpected syntax for \"" (-> args first trim) "\"")
:Messages [{:severity "Error"
:MessageText (str "Invalid syntax for " (first args))}]})
:unsupported (fn [& args] {"$type" "Unsupported"
:Command "unsupported"})
:unrecognized (fn [& args] (first args))
:generate (fn [& [varname expr condition]]
......@@ -79,7 +74,7 @@
:codelist codelist
:options options})
:codelist (fn [& args] args)
:value_label (fn [& [value label]] {value label})
:Value_label (fn [& [value label]] {value label})
:label_values (fn [& [varlist codelistname]]
(let [labels (get *codelists* codelistname)]
(cond->
......@@ -89,9 +84,8 @@
:Labels (map (fn [lab] {:Value (first lab) :Label (second lab)}) labels)}
(not labels) (assoc
:Messages
[{:MessageText (str "Undefined label list " codelistname)
:Severity "warning"}]
)
[{:Severity "warning"
:MessageText (str "Undefined label list " codelistname)}])
)
)
)
......@@ -179,8 +173,8 @@
)
:primary (fn [& [prim]]
(cond
(number? prim) {"$type" "NumericConstantExpression", :value prim}
(string? prim) {"$type" "StringConstantExpression", :value prim}
(number? prim) {"$type" "NumericConstantExpression", :Value prim}
(string? prim) {"$type" "StringConstantExpression", :Value prim}
:else prim
)
)
......@@ -196,7 +190,7 @@
:string (fn [& args] (apply str (rest (butlast (first args)))))
:missing (fn [& [value]]
{"$type" "MissingValueConstantExpression"
:value value})
:Value value})
:recode (fn [& [vars rules filter options]]
(let [varlist (recode-varlist (expand-varlist vars *variables*) (or options {}))]
{"$type" "Recode"
......@@ -332,7 +326,7 @@
{
:globals {}
:output []
:messages []
:Messages []
:stack (list)
:next 0
:codelists {}
......@@ -458,15 +452,15 @@
mod-other-vars (or (:mod-other-vars command-result) *other-vars*)
command-result (dissoc command-result :mod-varlist)
command-result (dissoc command-result :mod-other-vars)
command-result (assoc command-result :sourceInformation {:lineNumberStart (:next state) , :originalSourceText source-text} )
messages (:messages command-result)
command-result (dissoc command-result :messages)]
command-result (assoc command-result :SourceInformation {:LineNumberStart (:next state) , :OriginalSourceText source-text} )
messages (:Messages command-result)
command-result (dissoc command-result :Messages)]
(-> state
(assoc :variables (vec mod-varlist))
(assoc :other-vars mod-other-vars)
(update :output conj command-result)
(update :messages concat (map #(assoc % :lineNumber (:next state)) messages)))
(update :Messages concat (map #(assoc % :lineNumber (:next state)) messages)))
)
)
)
......@@ -498,7 +492,7 @@
next-line
(put-on-stack {:goto (inc (:next state))
:macro-name (second translation)
:values (rest expanded-varlist)})
:Values (rest expanded-varlist)})
(assoc-in [:globals (second translation)] (first expanded-varlist))
)
)
......@@ -507,7 +501,7 @@
next-line
(put-on-stack {:goto (inc (:next state))
:macro-name (second translation)
:values (rest local-list)})
:Values (rest local-list)})
(assoc-in [:globals (second translation)] (first local-list))
)
)
......@@ -516,7 +510,7 @@
next-line
(put-on-stack {:goto (inc (:next state))
:macro-name (second translation)
:values (rest expanded-numlist)})
:Values (rest expanded-numlist)})
(assoc-in [:globals (second translation)] (first expanded-numlist))
)
)
......@@ -525,7 +519,7 @@
next-line
(put-on-stack {:goto (inc (:next state))
:macro-name (second translation)
:values (rest list)})
:Values (rest list)})
(assoc-in [:globals (second translation)] (first list))
)
)
......@@ -542,7 +536,7 @@
next-line
(put-on-stack {:goto (inc (:next state))
:macro-name (second translation)
:values (rest values)})
:Values (rest values)})
(assoc-in [:globals (second translation)] (first values))
)
)
......@@ -562,13 +556,13 @@
)
)
:close_loop (let [top-of-stack (peek (:stack state))]
(if (:values top-of-stack)
(if-not (empty? (:values top-of-stack))
(if (:Values top-of-stack)
(if-not (empty? (:Values top-of-stack))
(-> state
(goto (:goto top-of-stack))
(assoc-in [:globals (:macro-name top-of-stack)] (first (:values top-of-stack)))
(assoc-in [:globals (:macro-name top-of-stack)] (first (:Values top-of-stack)))
pop-stack
(put-on-stack (assoc top-of-stack :values (rest (:values top-of-stack))))
(put-on-stack (assoc top-of-stack :Values (rest (:Values top-of-stack))))
)
(-> state
next-line
......@@ -634,12 +628,12 @@
[stata variables filename]
(let [final-state (do-it stata variables)]
(generate-string
{:CommandCount (count (:output final-state))
:parser "stata-sdtl-converter"
:sourceLanguage "stata"
:parserVersion (get-version)
:sourcefileName filename
:messages (:messages final-state)
{:SourceFileName filename
:SourceLanguage "stata"
:CommandCount (count (:output final-state))
:Messages (:Messages final-state)
:Parser "stata-sdtl-converter"
:ParserVersion (get-version)
:Commands (:output final-state)}
{:pretty true}
)
......
......@@ -50,15 +50,15 @@
})
(defn unsupported-func [funcname]
{:command "unsupported"
:message (str "Unsupported egen function \"" funcname "\"")
:messages [{:messageText (str "Unsupported egen function \"" funcname "\"")
:severity "error"}]}
{"$type" "Unsupported"
:Command "unsupported"
:Message (str "Unsupported egen function \"" funcname "\"")
:Messages [{:Severity "Error"
:MessageText (str "Unsupported egen function \"" funcname "\"")}]}
)
(defn make-varlist-expression [varlist]
{"$type" "VariableListExpression" :variables (map (fn [varname] {:variableName varname , "$type" "VariableSymbolExpression"}) (expand-varlist varlist *variables*))}
)
{"$type" "VariableListExpression" :Variables (map variable-symbol-expression (expand-varlist varlist *variables*))})
(defn make-compute [varname funcname args _filter]
{:command "compute"
......@@ -80,7 +80,7 @@
varname
funcname
[(make-varlist-expression varlist)
{"$type" "NumberListExpression" :values (->> (get-var-length-option :values 1 options) (get options) first)}]
{"$type" "NumberListExpression" :Values (->> (get-var-length-option :Values 1 options) (get options) first)}]
_filter
)
)
......@@ -97,9 +97,9 @@
:rowpctile (make-compute
varname
"rowpctile"
(let [p (:p options {:values [50]})]
(let [p (:p options {:Values [50]})]
[(make-varlist-expression varlist)
{"$type" "NumericConstantExpression" :value (-> p :values first)}]
{"$type" "NumericConstantExpression" :Value (-> p :Values first)}]
)
_filter)
:rowtotal (make-compute
......@@ -118,22 +118,22 @@
[(make-varlist-expression varlist)
(->> (get-var-length-option :format 1 options) (get options) first)
(->> (get-var-length-option :decode 1 options) nil? not)
(->> (get-var-length-option :maxlength 4 options) (get options) first :values first)
(->> (get-var-length-option :maxlength 4 options) (get options) first :Values first)
(->> (get-var-length-option :punct 1 options) (get options) first)]
_filter)
:cut (make-compute
varname
"cut"
[(first varlist)
(->> options :at (transduce (map #(:values %)) concat) (assoc {"$type" "NumberListExpression"} :values))
(->> (get-var-length-option :group 1 options) (get options) first :values first)
(->> options :at (transduce (map #(:Values %)) concat) (assoc {"$type" "NumberListExpression"} :Values))
(->> (get-var-length-option :group 1 options) (get options) first :Values first)
(->> (get-var-length-option :icodes 2 options) nil? not)
(->> (get-var-length-option :label 3 options) nil? not)]
_filter)
:anycount (make-any varname funcname varlist options _filter)
:anymatch (make-any varname funcname varlist options _filter)
:anyvalue (make-any varname funcname varlist options _filter)
; default
(make-compute
varname
......@@ -185,11 +185,11 @@
"std_val"
(let [m (get-var-length-option :mean 1 options)
s (get-var-length-option :std 1 options)
m (if m (get options m) {:values [0]})
s (if s (get options s) {:values [1]})]
m (if m (get options m) {:Values [0]})
s (if s (get options s) {:Values [1]})]
[expr
{"$type" "NumericConstantExpression" :value (-> m :values first)}
{"$type" "NumericConstantExpression" :value (-> s :values first)}]
{"$type" "NumericConstantExpression" :Value (-> m :Values first)}
{"$type" "NumericConstantExpression" :Value (-> s :Values first)}]
)
_filter)
:pctile (make-aggregate
......@@ -197,7 +197,7 @@
varname
(get aggregate-funcs (keyword funcname))
[expr
{"$type" "NumericConstantExpression" :value (-> (:p options {:values [50]}) :values first)}]
{"$type" "NumericConstantExpression" :Value (-> (:p options {:Values [50]}) :Values first)}]
_filter)
:pc (make-aggregate
by-clause
......@@ -212,7 +212,7 @@
(get aggregate-funcs (keyword funcname))
[expr
{"$type" "StringConstantExpression"
:value (cond
:Value (cond
(get-var-length-option :field 1 options) "field"
(get-var-length-option :track 1 options) "track"
(get-var-length-option :unique 1 options) "unique"
......
......@@ -183,8 +183,7 @@
:expression {"$type" "functionCallExpression"
:function (get collapse-stats (-> aggregation :aggstat keyword))
:isSdtlName true
:arguments (cond-> [{"$type" "variableSymbolExpression"
:variableName src}]
:arguments (cond-> [(variable-symbol-expression src)]
(:p aggregation) (conj (:p aggregation))
)
}
......
......@@ -10,6 +10,11 @@
(def ^:dynamic *codelists* {})
(def ^:dynamic *other-vars* {})
(defn variable-symbol-expression
[varname]
{"$type" "VariableSymbolExpression"
:VariableName varname})
(defn get-var-length-option [k l m]
(loop [opts (keys m)]
(let [looking-for (apply str (take (inc l) (str k)))
......
This diff is collapsed.
......@@ -44,33 +44,33 @@
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}}
:argumentValue {"$type" "VariableListExpression"
:Variables ({"$type" "VariableSymbolExpression"
:VariableName "a"}
{"$type" "VariableSymbolExpression"
:VariableName "b"})}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 50}})
:Value 50}})
:function "rowpctile"
:isSdtlName true}
:mod-varlist ["some-var"]
:variableNames ["some-var"]})
)
(is
(= (varlist-compute "some-var" :rowpctile ["a" "b"] 5 {:p {:values '(10)}})
(= (varlist-compute "some-var" :rowpctile ["a" "b"] 5 {:p {:Values '(10)}})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}}
:argumentValue {"$type" "VariableListExpression"
:Variables ({"$type" "VariableSymbolExpression"
:VariableName "a"}
{"$type" "VariableSymbolExpression"
:VariableName "b"})}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 10}})
:Value 10}})
:function "rowpctile"
:isSdtlName true}
:mod-varlist ["some-var"]
......@@ -82,11 +82,11 @@
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}}
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:VariableName "a"}
{"$type" "VariableSymbolExpression"
:VariableName "b"})}}
{:argumentName "EXP2"
:argumentValue false})
:function "rowtotal"
......@@ -100,11 +100,11 @@
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}}
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:VariableName "a"}
{"$type" "VariableSymbolExpression"
:VariableName "b"})}}
{:argumentName "EXP2"
:argumentValue true})
:function "rowtotal"
......@@ -118,11 +118,11 @@
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}}
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:VariableName "a"}
{"$type" "VariableSymbolExpression"
:VariableName "b"})}}
{:argumentName "EXP2"
:argumentValue false})
:function "tag"
......@@ -136,11 +136,11 @@
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}}
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:VariableName "a"}
{"$type" "VariableSymbolExpression"
:VariableName "b"})}}
{:argumentName "EXP2"
:argumentValue true})
:function "tag"
......@@ -154,11 +154,11 @@
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}})
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:VariableName "a"}
{"$type" "VariableSymbolExpression"
:VariableName "b"})}})
:function "diff"
:isSdtlName true}
:mod-varlist ["some-var"]
......@@ -176,10 +176,10 @@
:argumentValue 5}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 0}}
:Value 0}}
{:argumentName "EXP3"
:argumentValue {"$type" "NumericConstantExpression"
:value 1}})
:Value 1}})
:function "std_val"
:isSdtlName true}
:mod-varlist ["somevar"]
......@@ -187,7 +187,7 @@
)
)
(is
(= (egen-expr nil "somevar" "std" 5 1 {:mean {:values '(7)}})
(= (egen-expr nil "somevar" "std" 5 1 {:mean {:Values '(7)}})
'{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
......@@ -195,10 +195,10 @@
:argumentValue 5}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 7}}
:Value 7}}
{:argumentName "EXP3"
:argumentValue {"$type" "NumericConstantExpression"
:value 1}})
:Value 1}})
:function "std_val"
:isSdtlName true}
:mod-varlist ["somevar"]
......@@ -206,7 +206,7 @@
)
)
(is
(= (egen-expr nil "somevar" "std" 5 1 {:std {:values '(2)} :mean {:values '(7)}})
(= (egen-expr nil "somevar" "std" 5 1 {:std {:Values '(2)} :mean {:Values '(7)}})
'{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
......@@ -214,10 +214,10 @@
:argumentValue 5}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 7}}
:Value 7}}
{:argumentName "EXP3"
:argumentValue {"$type" "NumericConstantExpression"
:value 2}})
:Value 2}})
:function "std_val"
:isSdtlName true}
:mod-varlist ["somevar"]
......@@ -233,7 +233,7 @@
:argumentValue 5}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 50}})
:Value 50}})
:function "agg_pctile"
:isSdtlName true}
:variableNames ["somevar"]}]
......@@ -244,7 +244,7 @@
)
)
(is
(= (egen-expr ["a" "b"] "somevar" "pctile" 5 1 {:p {:values '(10)}})
(= (egen-expr ["a" "b"] "somevar" "pctile" 5 1 {:p {:Values '(10)}})
'{:aggregateVariables [{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
......@@ -252,7 +252,7 @@
:argumentValue 5}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 10}})
:Value 10}})
:function "agg_pctile"
:isSdtlName true}
:variableNames ["somevar"]}]
......@@ -267,12 +267,12 @@
(deftest egen-fill-test
(is
(=
(egen-fill "somevar" {:values '(1 2 3)} nil)
(egen-fill "somevar" {:Values '(1 2 3)} nil)
'{:command "compute"
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {:values (1
:argumentValue {:Values (1
2
3)}})
:function "fill"
......
......@@ -35,11 +35,11 @@
(deftest transform-recode-rule-test
(is
(= (transform-recode-rule [{:first 1, :last 4} 9] 7 "foo")
{:fromValue [9]
{:FromValue [9]
:fromValueRange [{:first 1
:last 4}]
:label "foo"
:to 7})
:Label "foo"
:To 7})
)
)
......
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