def test_parse_atoms(self) -> None: self.assertEqual(sexp.parse("hi"), [SSym("hi")]) self.assertEqual(sexp.parse("hi hey hoi"), [ SSym("hi"), SSym("hey"), SSym("hoi"), ]) self.assertEqual(sexp.parse("42 foo"), [SNum(42), SSym("foo")])
def test_threadLast(self): self.assertEqual(sexp.parse("(->> a b )"), ['b', 'a']) self.assertEqual(sexp.parse("(->> a b c )"), ['c', ['b', 'a']]) self.assertEqual(sexp.parse("(->> (a) (b) (c ))"), ['c', ['b', ['a']]]) self.assertEqual(sexp.parse("(->> (a) (b c) (d ))"), ['d', ['b', 'c', ['a']]]) self.assertEqual(sexp.parse("(->> a (b) (c) (->> d e f ))"), ['f', ['e', 'd'], ['c', ['b', 'a']]])
def test_tail_call_in_begin(self) -> None: prog = sexp.parse( '(define (vacuous-tail) (begin 42 43 (vacuous-tail)))') self.finder.visit(prog) self.assertEqual(1, len(self.finder.tail_calls)) self.assert_symbol_in_tail_calls(sexp.SSym('vacuous-tail'), self.finder.tail_calls)
def test_tail_call_in_conditional_both_branches(self) -> None: prog = sexp.parse( '(define (vacuous-tail) (if true (vacuous-tail) (vacuous-tail)))') self.finder.visit(prog) self.assertEqual(2, len(self.finder.tail_calls)) self.assert_symbol_in_tail_calls(sexp.SSym('vacuous-tail'), self.finder.tail_calls)
def parse_file(self, filename): #print filename, sys.stdout.write('.') sys.stdout.flush() with open(filename) as fin: footprint = None import sexp exp = sexp.parse(fin.read()) for e in exp: #print e if e[0][0] == 'module': name = e[1][0] #print name, footprint = Footprint(name=name) footprint.struct = e self.footprints[name] = footprint for i in e[2:]: if i[0][0] == 'pad': try: footprint.add_pad(i) except: print e raise return exp
def construct_from_sexp(self, content): ''' build the tree from s-expression :param content: :return: ''' parsed = sexp.parse(content) self.construct_from_sexp(parsed)
def main(): args = get_args() bom = BOM(args.xmlpath, include=args.include, exclude=args.exclude) with open(args.xmlpath[:-3] + "kicad_pcb") as f: pcb = PCB(sexp.parse(f.read())) mm_to_pt = 2.835 ps = cairo.PDFSurface(args.pdfpath, args.page_width*mm_to_pt, args.page_height*mm_to_pt) cr = cairo.Context(ps) # Scale user units to millimetres cr.scale(mm_to_pt, mm_to_pt) labels = sheet_positions(cr, args.label_width, args.label_height, args.labels_x, args.labels_y, args.margin_top, args.margin_left, args.spacing_x, args.spacing_y) suppliers = [name.strip() for name in args.suppliers.split(",")] for line in bom.lines: if line.supplier not in suppliers: continue if not line.footprint and not args.include_parts_without_footprint: continue label = next(labels) line.render(cr, (label[0]+1, label[1]), args.label_width-2, 14) sides = pcb.get_mod_sides(line.refs) if "F.Cu" in sides and "B.Cu" in sides: # if both sides present, split area and draw both pcb.render(cr, (label[0]+1, label[1]+14), (args.label_width-4)/2.0, args.label_height-14, ["F.Fab"], ["F.Cu", "*.Cu", "F.SilkS"], sides["F.Cu"]) pcb.render(cr, (label[0]+3+(args.label_width-3)/2.0, label[1]+14), (args.label_width-4)/2.0, args.label_height-14, ["B.Fab"], ["B.Cu", "*.Cu", "B.SilkS"], sides["B.Cu"], args.flip_vert) elif "F.Cu" in sides: pcb.render(cr, (label[0]+1, label[1]+14), args.label_width-2, args.label_height-14, ["F.Fab"], ["F.Cu", "*.Cu", "F.SilkS"], sides["F.Cu"]) elif "B.Cu" in sides: pcb.render(cr, (label[0]+1, label[1]+14), args.label_width-2, args.label_height-14, ["B.Fab"], ["B.Cu", "*.Cu", "B.SilkS"], sides["B.Cu"], args.flip_vert) cr.show_page()
def test_tail_call_in_one_branch_linear_call_in_other(self) -> None: prog = sexp.parse(''' (define (vacuous-tail) (if true (+ 1 (vacuous-tail)) (vacuous-tail)) )''') self.finder.visit(prog) self.assertEqual(1, len(self.finder.tail_calls)) self.assert_symbol_in_tail_calls(sexp.SSym('vacuous-tail'), self.finder.tail_calls)
def test_function_def(self) -> None: prog = '(define (funcy spam egg) (+ spam egg)) (funcy 42 43)' self.assertEqual([ SFunction( SSym('funcy'), [SSym('spam'), SSym('egg')], sexp.to_slist([SCall(SSym('+'), [SSym('spam'), SSym('egg')])])), SCall(SSym('funcy'), [SNum(42), SNum(43)]) ], sexp.parse(prog))
def compile(self, s): try: code = parser.parse(s) if isinstance(s, str) else s ast = map(self._compile, self.intp.preprocess(code)) pkg = self._package(ast) if debug > -1: print_code(ast) return Environment(pkg, self.context) except Exception as e: import traceback traceback.print_exc()
def prepare_v1(glob): # remove comments glob = re.sub(r';.*?[\r\n]', '', glob) x = sexp.parse(glob) if True: print "raw parse:" print x outarr = [] prettyprint(x, outarr) for out in outarr: print out
def prepare_v1(glob): # remove comments glob = re.sub(r';.*?[\r\n]','',glob) x = sexp.parse(glob) if True: print "raw parse:" print x outarr=[] prettyprint(x,outarr) for out in outarr: print out
def test_lambda(self) -> None: prog = '(lambda (spam egg) (+ spam egg)) (lambda () 42)' self.assertEqual([ SFunction( SSym('__lambda0'), [SSym('spam'), SSym('egg')], sexp.to_slist([SCall(SSym('+'), [SSym('spam'), SSym('egg')])]), is_lambda=True), SFunction(SSym('__lambda1'), [], sexp.to_slist([SNum(42)]), is_lambda=True), ], sexp.parse(prog))
def test_lambda_called_inline(self) -> None: self.maxDiff = None prog = '((lambda (spam egg) (+ spam egg)) 42 43)' self.assertEqual([ SCall( SFunction(SSym('__lambda0'), [SSym('spam'), SSym('egg')], sexp.to_slist( [SCall(SSym('+'), [SSym('spam'), SSym('egg')])]), is_lambda=True), [SNum(42), SNum(43)]) ], sexp.parse(prog))
def run(self, s, mult=True): global debug if debug == -1: Lisp.preprocess_ = self.preprocess_._orig Lisp.eval = self.eval._orig Lisp.quasieval = self.quasieval._orig debug = -2 try: if isinstance(s, str): s = parser.parse(s) except SyntaxError as e: self.vars["signal"](self, ["error", "standard", "syntax"], *e.args) if not mult: s = [s] return map(lambda x: self.eval(self.preprocess(x)), s)[-1] if s else None
def prepare_test(glob): # change semicolon comments to (comment "comment") glob = re.sub(r'(;.*?)([\r\n])',r'(comment "\1")\2',glob) x = sexp.parse(glob) if True: print "raw parse:" print x from pprint import pprint with codecs.open("temp2","w","utf-8") as f: pprint(x,f) outarr=[] alterparse(x,outarr) with codecs.open("temp2","a","utf-8") as f: f.write("\nALTERPARSE\n\n") pprint(outarr,f) return outarr
def prepare(glob,testflag=False): # try to convert array literals to lists glob = re.sub(r'\[','(array ',glob) glob = re.sub(r'\]',')',glob) # change semicolon comments to (comment "comment") glob = prepare_comment(glob) # change \" to some unique string (say DOUBLEQUOTE) unique = (r'\\"','DOUBLEQUOTE') glob = re.sub(unique[0],unique[1],glob) # use sexp to parse x = sexp.parse(glob) # 'improve' what sexp did outarr=[] alterparse(x,outarr,unique) return outarr
def test_visit_begin(self) -> None: prog = sexp.parse('(begin egg 42)') recorder = TraversalRecorder() recorder.visit(prog) expected = [ 'SBegin', 'SSym', 'SNum', ] self.assertEqual(expected, recorder.exprs) counter = ExpressionCounter() counter.visit(prog) self.assertEqual(len(expected), counter.num_exprs)
def prepare_test(glob): # change semicolon comments to (comment "comment") glob = re.sub(r'(;.*?)([\r\n])', r'(comment "\1")\2', glob) x = sexp.parse(glob) if True: print "raw parse:" print x from pprint import pprint with codecs.open("temp2", "w", "utf-8") as f: pprint(x, f) outarr = [] alterparse(x, outarr) with codecs.open("temp2", "a", "utf-8") as f: f.write("\nALTERPARSE\n\n") pprint(outarr, f) return outarr
def prepare(glob, testflag=False): # try to convert array literals to lists glob = re.sub(r'\[', '(array ', glob) glob = re.sub(r'\]', ')', glob) # change semicolon comments to (comment "comment") glob = prepare_comment(glob) # change \" to some unique string (say DOUBLEQUOTE) unique = (r'\\"', 'DOUBLEQUOTE') glob = re.sub(unique[0], unique[1], glob) # use sexp to parse x = sexp.parse(glob) # 'improve' what sexp did outarr = [] alterparse(x, outarr, unique) return outarr
def test_visit_vect(self) -> None: prog = sexp.parse('[1 spam true]') recorder = TraversalRecorder() recorder.visit(prog) expected = [ 'SVect', 'SNum', 'SSym', 'SBool', ] self.assertEqual(expected, recorder.exprs) counter = ExpressionCounter() counter.visit(prog) self.assertEqual(len(expected), counter.num_exprs)
def test_visit_conditional(self) -> None: prog = sexp.parse('(if true 42 nope)') recorder = TraversalRecorder() recorder.visit(prog) expected = [ 'SConditional', 'SBool', 'SNum', 'SSym', ] self.assertEqual(expected, recorder.exprs) counter = ExpressionCounter() counter.visit(prog) self.assertEqual(len(expected), counter.num_exprs)
def test_quote(self) -> None: self.assertEqual([Quote(SSym('spam'))], sexp.parse("'spam")) self.assertEqual([Quote(Nil)], sexp.parse("'()")) self.assertEqual([Quote(Nil)], sexp.parse("(quote ())")) self.assertEqual([ Quote(sexp.to_slist( [SSym('if'), SBool(True), SNum(2), SNum(3)])) ], sexp.parse("'(if true 2 3)")) self.assertEqual( [Quote(sexp.to_slist([SNum(1), SNum(2), SNum(3)]))], sexp.parse("(quote (1 2 3))")) self.assertEqual(sexp.parse("'(1 2 3)"), sexp.parse("(quote (1 2 3))")) self.assertEqual(str(sexp.parse("(quote (1 2 3))")[0]), "'(1 2 3)")
def test_visit_lambda(self) -> None: prog = sexp.parse('(lambda (egg sausage) false egg)') recorder = TraversalRecorder() recorder.visit(prog) expected = [ 'SFunction', 'SSym', 'SSym', 'SBool', 'SSym', ] self.assertEqual(expected, recorder.exprs) counter = ExpressionCounter() counter.visit(prog) self.assertEqual(len(expected), counter.num_exprs)
def test_visit_inline_called_lambda(self) -> None: prog = sexp.parse('((lambda (egg) egg) 42)') recorder = TraversalRecorder() recorder.visit(prog) expected = [ 'SCall', 'SFunction', 'SSym', 'SSym', 'SNum', ] self.assertEqual(expected, recorder.exprs) counter = ExpressionCounter() counter.visit(prog) self.assertEqual(len(expected), counter.num_exprs)
def run(env: EvalEnv, text: str, context: str = "top-level") -> Value: """ Run a piece of code in an environment, returning its result. >>> env = EvalEnv() >>> add_intrinsics(env) >>> add_builtins(env) >>> add_prelude(env) >>> run(env, '(+ 1 1)') SNum(value=2) >>> run(env, '(> (vector-length (cons 1 [])) 3)') SBool(value=False) """ code = sexp.parse(text) result: Value = sexp.SVect([]) for part in code: result = run_code(env, part, context=context) return result
def test_visit_quote(self) -> None: prog = sexp.parse('(quote (1 spam true))') recorder = TraversalRecorder() recorder.visit(prog) expected = [ 'Quote', 'SPair', 'SNum', 'SPair', 'SSym', 'SPair', 'SBool', ] self.assertEqual(expected, recorder.exprs) counter = ExpressionCounter() counter.visit(prog) self.assertEqual(len(expected), counter.num_exprs)
def test_fib_tail(self) -> None: prog = sexp.parse(''' (define (fib-tail n) (fib-tail-impl n 0 1) ) (define (fib-tail-impl n first second) (if (= n 0) first (if (= n 1) second (fib-tail-impl (- n 1) second (+ first second)) ) ) )''') self.finder.visit(prog) self.assertEqual(1, len(self.finder.tail_calls)) self.assert_symbol_in_tail_calls(sexp.SSym('fib-tail-impl'), self.finder.tail_calls)
def test_comments(self) -> None: prog = """ ;;; We want to define a cool function here! (define ; hi ;; A function name (cool-func x y) ; wow ;; branches are cheaper than subtraction, right? :P (if (= x y) 0 (- x y))) """ self.assertEqual([ SFunction( SSym('cool-func'), [SSym('x'), SSym('y')], sexp.to_slist([ SConditional( SCall(SSym('='), [SSym('x'), SSym('y')]), SNum(0), SCall(SSym('-'), [SSym('x'), SSym('y')])) ])) ], sexp.parse(prog))
def test_no_tail_calls_in_lambdas_dlist_code(self) -> None: prog = sexp.parse(''' (define (dlist-push-front dlist datum) (if (dlist-empty dlist) ((lambda (dlist new-node) (dlist-set-first! dlist new-node) (dlist-set-last! dlist new-node) ) dlist (node-make datum 0 0)) ((lambda (dlist current-first new-node) (node-set-prev! current-first new-node) (dlist-set-first! dlist new-node) ) dlist (dlist-first dlist) (node-make datum 0 (dlist-first dlist))) ) ) ''') self.finder.visit(prog) self.assertEqual(0, len(self.finder.tail_calls))
def main(): args = get_args() bom = BOM(args.xmlpath) with open(args.xmlpath[:-3] + "kicad_pcb") as f: pcb = PCB(sexp.parse(f.read())) mm_to_pt = 2.835 ps = cairo.PDFSurface(args.pdfpath, args.page_width*mm_to_pt, args.page_height*mm_to_pt) cr = cairo.Context(ps) # Scale user units to millimetres cr.scale(mm_to_pt, mm_to_pt) labels = sheet_positions(cr, args.label_width, args.label_height, args.labels_x, args.labels_y, args.margin_top, args.margin_left, args.spacing_x, args.spacing_y) suppliers = [name.strip() for name in args.suppliers.split(",")] for line in bom.lines: if line.supplier not in suppliers: continue if not line.footprint and not args.include_parts_without_footprint: continue label = next(labels) line.render(cr, (label[0]+1, label[1]), args.label_width-2, 14) pcb.render(cr, (label[0]+1, label[1]+14), args.label_width-2, args.label_height-14, line.refs) cr.show_page()
def main(xmlpath, pdfpath): bom = BOM(xmlpath) with open(xmlpath[:-3] + "kicad_pcb") as f: pcb = PCB(sexp.parse(f.read())) mm_to_pt = 2.835 ps = cairo.PDFSurface(pdfpath, page_width * mm_to_pt, page_height * mm_to_pt) cr = cairo.Context(ps) # Scale user units to millimetres cr.scale(1 / 0.3528, 1 / 0.3528) labels = sheet_positions( cr, label_width, label_height, labels_x, labels_y, margin_top, margin_left, spacing_x, spacing_y ) for line, label in zip(bom.lines, labels): if line.supplier in suppliers_to_output: line.render(cr, (label[0] + 1, label[1]), label_width - 2, 14) pcb.render(cr, (label[0] + 1, label[1] + 14), label_width - 2, label_height - 14, line.refs) cr.show_page()
def _compile(self, s): if isinstance(s, str): s = parser.parse(s) s = self.intp.preprocess(s) a = map(self._tostmt, s) if self.interactive: a = ast.Interactive(a) t = "single" elif len(a) == 1 and isinstance(a[0], ast.Expr): a = ast.Expression(a[0].value) t = "eval" else: a = ast.Module(a) t = "exec" ast.fix_missing_locations(a) try: return compile(a, "<pylisp>", t) except ValueError: if debug > -1: print "Error Compiling Code!" print ast.dump(a) raise Exception
def main(xmlpath, pdfpath): bom = BOM(xmlpath) with open(xmlpath[:-3] + "kicad_pcb") as f: pcb = PCB(sexp.parse(f.read())) mm_to_pt = 2.835 ps = cairo.PDFSurface(pdfpath, page_width*mm_to_pt, page_height*mm_to_pt) cr = cairo.Context(ps) # Scale user units to millimetres cr.scale(1/0.3528, 1/0.3528) labels = sheet_positions(cr, label_width, label_height, labels_x, labels_y, margin_top, margin_left, spacing_x, spacing_y) for line, label in zip(bom.lines, labels): if line.supplier in suppliers_to_output: line.render(cr, (label[0]+1, label[1]), label_width-2, 14) pcb.render(cr, (label[0]+1, label[1]+14), label_width-2, label_height-14, line.refs) cr.show_page()
def test_visit_call(self) -> None: prog = sexp.parse('(define (spam egg sausage) egg) (spam 42 true)') recorder = TraversalRecorder() recorder.visit(prog) expected = [ 'SFunction', 'SSym', 'SSym', 'SSym', 'SSym', 'SCall', 'SSym', 'SNum', 'SBool', ] self.assertEqual(expected, recorder.exprs) counter = ExpressionCounter() counter.visit(prog) self.assertEqual(len(expected), counter.num_exprs)
def read(s=""): import sexp v = raw_input(s) return list(sexp.parse(v))
# # main # # dump processed defs def dump(defs): for d in defs: print '\n' + d.dump() # generator functions callable from the command line gens = { 'defs': gendefs, # generate code 'dump': dump # dump internal reps } if len(sys.argv) > 1 and sys.argv[1] in gens: gen = gens[sys.argv[1]] else: print "Error: must specify defs or dump as command-line argument" sys.exit(1) try: sexprs = [sexpr for sexpr in parse(hrdefs) if isValid(sexpr)] defs = [toDef(sexpr) for sexpr in sexprs] process(defs) gen(defs) except ParseError as e: print 'parse error: %s' % e.message() sys.exit(1)
def to_sexp(self): o = [] o.append( [('net',), (0,), ('""',)]) for n in self.nets: o.append( [('net',), (n,), (self.nets[n],)]) oo = [] oo.append( [('clearance',), ('0.1',)] ) oo.append( [('trace_width',), ('0.25',)] ) oo.append( [('via_dia',), ('0.6',)] ) oo.append( [('via_drill',), ('0.4',)] ) oo.append( [('uvia_dia',), ('0.3',)] ) oo.append( [('uvia_drill',), ('0.1',)] ) for n in sorted(map(int,self.nets)): oo.append( [('add_net',), (self.nets[n],)]) o.append( [('net_class',), ('Default',) ,('\"this is default net class\"',)] + oo ) for p in self.packages: o.append( self.packages[p].to_sexp() ) o = [('kicad_pcb',), [('version',), ('4',)], [('host',), ('libboard',), ('0.0.1',)]] +\ [[('general',), [('links',), ('7',)], [('area',), ('7',)], [('thickness',), ('1.6',)], [('drawings',), ('4',)], [('tracks',), ('0',)], [('zones',), ('0',)], [('modules',), ('{}'.format(len(self.nets.values())),)], [('nets',), ('7',)], [('no_connects',), ('6',)]]] + sexp.parse("""(layers (0 F.Cu signal)""" + \ '\n'.join(['({} In{}.Cu signal)'.format(i, i) for i in range(self.layer_count-2)]) + \ # (1 In1.Cu signal) # (2 In2.Cu signal) # (3 In3.Cu signal) # (4 In4.Cu signal) # (5 In5.Cu signal) # (6 In6.Cu signal) # (7 In7.Cu signal) # (8 In8.Cu signal) # (9 In9.Cu signal) # (10 In10.Cu signal) # (11 In11.Cu signal) # (12 In12.Cu signal) # (13 In13.Cu signal) # (14 In14.Cu signal) # (15 In15.Cu signal) # (16 In16.Cu signal) # (17 In17.Cu signal) # (18 In18.Cu signal) # (19 In19.Cu signal) # (20 In20.Cu signal) # (21 In21.Cu signal) # (22 In22.Cu signal) # (23 In23.Cu signal) # (24 In24.Cu signal) # (25 In25.Cu signal) # (26 In26.Cu signal) # (27 In27.Cu signal) # (28 In28.Cu signal) # (29 In29.Cu signal) # (30 In30.Cu signal) """ (31 B.Cu signal) (32 B.Adhes user) (33 F.Adhes user) (34 B.Paste user) (35 F.Paste user) (36 B.SilkS user) (37 F.SilkS user) (38 B.Mask user) (39 F.Mask user) (40 Dwgs.User user) (41 Cmts.User user) (42 Eco1.User user) (43 Eco2.User user) (44 Edge.Cuts user) (45 Margin user) (46 B.CrtYd user) (47 F.CrtYd user) (48 B.Fab user) (49 F.Fab user) )""") + sexp.parse("""(page A4) (setup (last_trace_width 0.25) (trace_clearance 0.2) (zone_clearance 0.508) (zone_45_only no) (trace_min 0.2) (segment_width 0.2) (edge_width 0.1) (via_size 0.6) (via_drill 0.4) (via_min_size 0.4) (via_min_drill 0.3) (uvia_size 0.3) (uvia_drill 0.1) (uvias_allowed no) (uvia_min_size 0.2) (uvia_min_drill 0.1) (pcb_text_width 0.3) (pcb_text_size 1.5 1.5) (mod_edge_width 0.15) (mod_text_size 1 1) (mod_text_width 0.15) (pad_size 1.5 1.5) (pad_drill 0.6) (pad_to_mask_clearance 0) (aux_axis_origin 0 0) (visible_elements FFFFFF7F) (pcbplotparams (layerselection 0x00030_80000001) (usegerberextensions false) (excludeedgelayer true) (linewidth 0.100000) (plotframeref false) (viasonmask false) (mode 1) (useauxorigin false) (hpglpennumber 1) (hpglpenspeed 20) (hpglpendiameter 15) (hpglpenoverlay 2) (psnegative false) (psa4output false) (plotreference true) (plotvalue true) (plotinvisibletext false) (padsonsilk false) (subtractmaskfromsilk false) (outputformat 1) (mirror false) (drillshape 1) (scaleselection 1) (outputdirectory ".")) )""") + o return o
def construct_from_sexp(self, content): '''build the tree from s-expression''' parsed = sexp.parse(content) self.construct_from_list(parsed)
def test_sexp_exception(): for sexp_input, parsed in testcases: assert sexp.parse(sexp_input) == parsed with pytest.raises(ValueError): sexp.parse('(1 (2 3)')
def validate(exp): """ validate if a s-expression is a valid query""" s = sexp.parse(exp) j = {"query": s} jsonschema.validate(j, schema) return j
def eval(self, path): if preprocess_only: import sexp map(self.l.preprocess, sexp.parse(open(path).read())) else: self.l.run(open(path).read())