Commit 3a14ac40 authored by Sigbjørn Revheim's avatar Sigbjørn Revheim
Browse files

Adding attribute to most commands

parent b6036f13
......@@ -33,31 +33,38 @@
:options (fn [& args] (reduce #(assoc %1 (keyword (first %2)) (apply concat (second %2))) {} args))
:option (fn [name & args] [name args])
:option_param (fn [& args] args)
:analysis (fn [& args] {:command "analysis"
:analysis (fn [& args] {"$type" "Analysis"
:Command "analysis"
:message (str "Ignoring \"" (-> args first trim) "\"")})
:invalid (fn [& args] {:command "invalid"
: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] {:command "unsupported"})
:unsupported (fn [& args] {"$type" "Unsupported"
:Command "unsupported"})
:unrecognized (fn [& args] (first args))
:generate (fn [& [varname expr condition]]
{:command "compute"
{"$type" "Compute"
:Command "compute"
:variableNames [varname]
:expression expr
:condition condition
:mod-varlist (conj *variables* varname)})
:replace (fn [& [varname expr condition]]
{:command "compute"
{"$type" "Compute"
:Command "compute"
:variableNames [varname]
:expression expr
:condition condition})
:label_variable (fn [& [varname label]]
{:command "setVariableLabel"
{"$type" "SetVariableLabel"
:Command "setVariableLabel"
:variableName varname
:label label})
:label_define (fn [& [defname codelist options]]
{:name defname
{"$type" ""
:name defname
:codelist codelist
:options options})
:codelist (fn [& args] args)
......@@ -65,7 +72,8 @@
:label_values (fn [& [varlist codelistname]]
(let [labels (get *codelists* codelistname)]
(cond->
{:command "setValueLabels"
{"$type" "SetValueLabels"
:Command "setValueLabels"
:variables (expand-varlist varlist *variables*)
:labels (map (fn [lab] {:value (first lab) :label (second lab)}) labels)}
(not labels) (assoc
......@@ -78,45 +86,54 @@
)
:by_clause (fn [& args] (first args))
:use (fn [& [filename]]
{:command "load"
{"$type" "Load"
:Command "load"
:filename filename
:mod-varlist (get *other-vars* filename (:default *other-vars*))})
:save (fn [& [filename]]
{:command "save"
{"$type" "Save"
:Command "save"
:filename filename
:software "stata"
:mod-other-vars (assoc *other-vars* filename *variables*)})
:quoted_use_arg (fn [& args] (first args))
:order_vars (fn [& [varlist options]]
(let [newlist (reorder-variables (expand-varlist varlist *variables*) options)]
{:command "order"
{"$type" ""
:Command "order"
:variables (expand-varlist varlist *variables*)
:how (keys options)
:result (diff-varlists *variables* newlist)
:mod-varlist newlist}))
:format_vars (fn [& args] {:command "setDisplayFormat"
:format_vars (fn [& args] {"$type" "SetDisplayFormat"
:Command "setDisplayFormat"
:format (if (-> args first string?) (first args) (second args))
:variables (expand-varlist (if (-> args first string?) (second args) (first args)) *variables*)})
:drop_cases (fn [& [expr]]
{:command "select"
{"$type" "Select"
:Command "select"
:condition {:function "not"
:isSdtlName true
:arguments [expr]}})
:drop_vars (fn [& [varlist]]
(let [expanded (expand-varlist varlist *variables*)]
{:command "deleteVariables"
{"$type" "DeleteVariables"
:Command "deleteVariables"
:variables expanded
:mod-varlist (vec (filter (comp not (set expanded)) *variables*))}
))
:keep_cases (fn [& [expr]]
{:command "select"
{"$type" "Select"
:Command "select"
:condition expr})
:keep_vars (fn [& [varlist]]
(let [expanded (expand-varlist varlist *variables*)]
{:command "keepVariables"
{"$type" "KeepVariables"
:Command "keepVariables"
:variables expanded
:mod-varlist (vec (filter (set expanded) *variables*))}))
:rename (fn [& args] {:command "rename"
:rename (fn [& args] {"$type" "Rename"
:Command "rename"
:renames [{
:oldName (first args)
:newName (second args)
......@@ -149,7 +166,7 @@
:primary (fn [& [prim]]
(cond
(number? prim) {"$type" "NumericConstantExpression" , :value prim}
(string? prim) {"$type" "StringConstantExpression" , :value prim}
(string? prim) {"$type" "StringConstantExpression" , :value prim}
:else prim
)
)
......@@ -168,27 +185,31 @@
:value value})
:recode (fn [& [vars rules filter options]]
(let [varlist (recode-varlist (expand-varlist vars *variables*) (or options {}))]
{:command "recode"
{"$type" "Recode"
:Command "recode"
:recodedVariables varlist
:rules (apply-options-to-rules rules (or options {}))
:mod-varlist (vec (concat *variables* (extract-target-variables varlist)))}))
:simple_recode (fn [& [varname rules filter options]]
(let [varlist (recode-varlist [varname] (or options {}))]
{:command "recode"
{"$type" "Recode"
:Command "recode"
:recodedVariables varlist
:rules (apply-options-to-rules rules (or options {}))
:mod-varlist (vec (concat *variables* (extract-target-variables varlist)))}))
:reshape_long (fn [& [stublist options]]
(if stublist
(let [make (reduce (fn [a b] (assoc a b (filter #(starts-with? % b) *variables*))) {} stublist)]
{:command "reshape_long"
{"$type" "ReshapeLong"
:Command "reshape_long"
:make make
:index_values (set (map #(. Integer parseInt (re-find #"\d+" %)) (reduce #(apply conj %1 %2) [] (vals make))))
:index_varname (or (first (first (:j options))) "_j")
:id_varname (first (first (:i options)))}
)
; else
{:command "reshape_long"}
{"$type" "ReshapeLong"
:Command "reshape_long"}
)
)
:reshape_wide (fn [& _] (throw (UnsupportedOperationException. "reshape wide modifies variable list in an unpredictable way. Cannot finish processing")))
......@@ -209,7 +230,8 @@
gen-merge (get-var-length-option :generate 3 options)
keep-using (get-var-length-option :keepusing 6 options)
other-vars (get *other-vars* (nth args 2))]
{:command "merge"
{"$type" ""
:Command "merge"
:mod-varlist (concat
*variables*
(cond->> (filter (comp not (set *variables*)) other-vars)
......@@ -254,7 +276,8 @@
true (concat *variables*)
true distinct
)]
{:command "append"
{"$type" ""
:Command "append"
:files (first args)
:options (second args)
:mod-varlist varlist
......@@ -297,7 +320,8 @@
:egen_fill egen-fill
:egen_mtr egen-mtr
:egen_expr egen-expr
:comment (fn [& args] {:command "comment"
:comment (fn [& args] {"$type" "Comment"
:Command "comment"
:commentText (trim (first args))})
}
)
......@@ -608,13 +632,13 @@
[stata variables filename]
(let [final-state (do-it stata variables)]
(generate-string
{:commandCount (count (:output final-state))
{:CommandCount (count (:output final-state))
:parser "stata-sdtl-converter"
:sourceLanguage "stata"
:parserVersion (get-version)
:sourcefileName filename
:messages (:messages final-state)
:commands (:output final-state)}
:Commands (:output final-state)}
{:pretty true}
)
)
......
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