Parse models v1 - sections
[clnl] / src / main / model.lisp
1 (in-package #:clnl-model)
2
3 (defvar *separator* "@#$#@#$#@")
4
5 (defstruct model
6  code
7  interface
8  info
9  turtle-shapes
10  version
11  preview-commands
12  system-dynamics
13  behavior-space
14  hub-net-client
15  link-shapes
16  model-settings
17  delta-tick)
18
19 (defun read-from-nlogo (str)
20  "READ-FROM-NLOGO STR => MODEL
21
22 ARGUMENTS AND VALUES:
23
24   STR: a readable stream
25   MODEL: an object representing the model
26
27 DESCRIPTION:
28
29   Takes a stream STR, reads in a nlogo file, parses it, and then
30   returns the model object."
31  (let
32   ((sections
33     (labels
34      ((read-sections (&optional section)
35        (let
36         ((line (read-line str nil)))
37         (when line
38          (if (string= *separator* line)
39           (cons section (read-sections))
40           (read-sections (append section (list line))))))))
41      (read-sections))))
42   (make-model
43    :code (nth 0 sections)
44    :interface (nth 1 sections)
45    :info (nth 2 sections)
46    :turtle-shapes (nth 3 sections)
47    :version (nth 4 sections)
48    :preview-commands (nth 5 sections)
49    :system-dynamics (nth 6 sections)
50    :behavior-space (nth 7 sections)
51    :hub-net-client (nth 8 sections)
52    :link-shapes (nth 9 sections)
53    :model-settings (nth 10 sections)
54    :delta-tick (nth 11 sections))))