Debian Codegraph @

lib/expr/exopen.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 /*
12 * Glenn Fowler
13 * AT&T Research
14 *
15 * expression library
16 */
17
18 #include "config.h"
19
20 #include <expr/exlib.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 /// destructor for `Exid_t`
26 static void free_exid(void *p) {
27 Exid_t *const exid = p;
28
29 // the `Exid_t` itself was allocated through the program’s `vm` allocator, so
30 // will be automatically deallocated, but we need to manually clean up the
31 // `local` set that is used for arrays
32 dtclose(exid->local);
33 }
34
35 /*
36 * allocate a new expression program environment
37 */
38
39 Expr_t*
40 exopen(Exdisc_t* disc)
41 {
42 Expr_t* program;
43 Exid_t* sym;
44
45 if (!(program = calloc(1, sizeof(Expr_t))))
46 return 0;
47 static Dtdisc_t symdisc = {.key = offsetof(Exid_t, name), .freef = free_exid};
48 if (!(program->symbols = dtopen(&symdisc, Dtset))) {
49 exclose(program);
50 return 0;
51 }
52 program->id = "libexpr:expr";
53 program->disc = disc;
54 setcontext(program);
55 program->file[0] = stdin;
56 program->file[1] = stdout;
57 program->file[2] = stderr;
58 program->main = (Exid_t){.lex = PROCEDURE, .index = PROCEDURE, .name = "main"};
59 dtinsert(program->symbols, &program->main);
60 for (sym = exbuiltin; *sym->name; sym++)
61 dtinsert(program->symbols, sym);
62 if ((sym = disc->symbols))
63 for (; *sym->name; sym++)
64 dtinsert(program->symbols, sym);
65 return program;
66 }