-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.ml
38 lines (28 loc) · 1.09 KB
/
codegen.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(* code generation *)
let chan = ref stdout
let set_chan new_chan = chan := new_chan
let gen v = output_string !chan v; output_string !chan "\n"
let gen_nonl v = output_string !chan v
let generate_begin () = gen
"#!/usr/bin/env python
# -*- coding: utf-8 -*-
\"\"\" File automatically generated by Ezreq \"\"\"
def print_ezreq(var):
try:
print(var.text)
except AttributeError:
print(var)
import requests
"
let generate_end () = gen
"
exit(0)"
let generate_comment t = gen_nonl ("# " ^ t)
let generate_assign t = gen_nonl (t ^ " = ")
let generate_get var url = gen (var ^ " = requests.get(" ^ url ^ ")\npipe_tmp = " ^ var)
let generate_get_pipe url = gen ("pipe_tmp = requests.get(" ^ url ^ ")")
let generate_extract var origin s e = gen (var ^ " = " ^ origin ^ ".text.split(" ^ s ^ ")[1].split(" ^ e ^ ")[0]")
let generate_extract_pipe s e = gen ("pipe_tmp = pipe_tmp.text.split(" ^ s ^ ")[1].split(" ^ e ^ ")[0]")
let generate_print var = gen ("print_ezreq(" ^ var ^ ")")
let generate_print_pipe () = gen ("print_ezreq(pipe_tmp)")
let finish_pipe var = gen (var ^ " = pipe_tmp")