All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
main.cc
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include <iostream>
6 
7 #include "common.h"
8 #include "chunk.h"
9 #include "vm.h"
10 #include "compile.h"
11 
12 #include "api.h"
13 
14 #include "../Data/RecoEvent.h"
15 #include "../Data/FlatInteraction.h"
16 
17 static void repl() {
18  numu::RecoEvent event;
19  event.type = (numu::MCType)1;
21  hit.pes = -5.6;
22  event.in_time_crt_hits.push_back(hit);
23 
24  numu::RecoSlice slice;
25  slice.flash_match.time = 2.;
26 
27  numu::TrueParticle particle;
28 
30  flat.ptrack.start[1] = 5.;
31 
32  uscript::Compiler::Register<numu::TrueParticle>();
33  uscript::Compiler::Register<numu::RecoEvent>();
34  uscript::Compiler::Register<numu::RecoSlice>();
35  uscript::Compiler::Register<numu::flat::FlatInteraction>();
36 
37  char line[1024];
38  uscript::VM vm;
39  while (1) {
40  std::cout << "> ";
41  if (!fgets(line, sizeof(line), stdin)) {
42  std::cout << "\n";
43  break;
44  }
46  if (uscript::Compiler::HadError()) continue;
47  vm.SetChunk(&chunk);
48  vm.AddGlobal("flat", &flat);
49  vm.AddGlobal("event", &event);
50  vm.AddGlobal("slice", &slice);
51  vm.AddGlobal("particle", &particle);
52  int global = 5;
53  vm.AddGlobal("int", &global);
55  vm.Run(&v);
56  if (!IS_NIL(v)) {
57  v.Print();
58  std::cout << std::endl;
59  }
60  }
61 }
62 
63 static char* readFile(const char* path) {
64  FILE* file = fopen(path, "rb");
65  if (file == NULL) {
66  std::cerr << "Could not open file: " << path << std::endl;
67  return NULL;
68  }
69 
70  fseek(file, 0L, SEEK_END);
71  size_t fileSize = ftell(file);
72  rewind(file);
73 
74  char* buffer = (char*)malloc(fileSize + 1);
75  if (buffer == NULL) {
76  std::cerr << "Not enough memory to read file\n";
77  return NULL;
78  }
79  size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
80  if (bytesRead < fileSize) {
81  std::cerr << "Could not read file.\n";
82  free(buffer);
83  return NULL;
84  }
85  buffer[bytesRead] = '\0';
86 
87  fclose(file);
88  return buffer;
89 }
90 
91 static int runFile(const char* path) {
92  char* source = readFile(path);
93  if (source == NULL) return 60;
94  uscript::VM vm;
95  uscript::InterpretResult result = vm.Interpret(source);
96  free(source);
97 
98  if (result == uscript::INTERPRET_COMPILE_ERROR) return 65;
99  if (result == uscript::INTERPRET_RUNTIME_ERROR) return 70;
100  return 0;
101 }
102 
103 int main(int argc, const char* argv[]) {
104  if (argc == 1) {
105  repl();
106  }
107  else if (argc == 2) {
108  return runFile(argv[1]);
109  }
110  else {
111  std::cout << "Usage: uscript [path]\n";
112  return 64;
113  }
114  return 0;
115 }
116 
117 
static bool HadError()
Definition: compile.h:139
void SetChunk(const Chunk *_chunk)
Definition: vm.cc:31
Chunk compileChunk(const char *source)
Definition: api.h:45
BEGIN_PROLOG could also be cerr
do source
* file
Definition: file_to_url.sh:69
static void repl()
Definition: main.cc:17
static int runFile(const char *path)
Definition: main.cc:91
process_name hit
Definition: cheaterreco.fcl:51
Definition: vm.h:20
BEGIN_PROLOG triggeremu_data_config_icarus settings PMTADCthresholds sequence::icarus_stage0_multiTPC_TPC physics sequence::icarus_stage0_EastHits_TPC physics sequence::icarus_stage0_WestHits_TPC physics producers purityana0 caloskimCalorimetryCryoE physics caloskimCalorimetryCryoW physics path
InterpretResult Interpret(const char *source)
Definition: vm.cc:14
FlashMatch flash_match
Result of flash matching algorithm on this slice.
Definition: RecoEvent.h:30
static char * readFile(const char *path)
Definition: main.cc:63
InterpretResult Run(Value *ret=NULL)
Definition: vm.cc:65
#define IS_NIL(value)
Definition: value.h:59
InterpretResult
Definition: vm.h:14
void Print() const
Definition: value.cc:5
float pes
Number of PE&#39;s in hit.
Definition: DetInfo.h:15
void AddGlobal(const char *name, const TObj *object)
Definition: vm.h:51
MCType
Definition: MCType.h:5
int main(int argc, char **argv)
BEGIN_PROLOG could also be cout