File size: 2,360 Bytes
8739cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

function PREPARECHEADER(script, syntaxcheck)  
  --parse the structures
  
  local definedStructures={} --list of structure objects from dissectStructurePath

  for i=0,script.Count-1 do    
    local l=script[i]:trim()
    
    if l:upper()=='USEMONO()' then --PREPARECHEADER runs before USEMONO is handled
      LaunchMonoDataCollector() 
    end
    
    if l:upper():startsWith('PREPARECHEADER(') then   
      
      if l:endsWith(')')==false then error('PREPARECHEADER syntax error') end      
      local paramsstr=l:sub(16,-2)
      local params={}
      for x in paramsstr:gmatch("[^,]+") do
        table.insert(params, x)
      end  
      
      if #params==0 then
        error('Error in '..l..': Not enough parameters');
      end
      --param1 is the base structure type. First check if it is already defined in definedStructures
      local structname=params[1]
      table.remove(params,1) --the rest is fieldnames
      
      local currentStructure=nil
      for j=1,#definedStructures do
        if definedStructures[i].Name:upper()==structname:upper() then
          currentStructure=definedStructures[i]
          break;
        end
      end
      
      if currentStructure==nil then
       -- printf("dissecting %s", structname)
        currentStructure,err=dissectStructurePath(structname, params) 
        
        if (currentStructure==nil) then
          _G.lastparams=params
          error('Failure dissecting '..structname..' ('..err..')')
        end
        table.insert(definedStructures,currentStructure)
      else
        --printf("appending to %s",currentStructure.Name)
        currentStructure=dissectStructurePath(currentStructure, params)
      end
      
      script[i]='' --remove the line
    end
  end
  
  if #definedStructures>0 then
  
    --convert the definedStructures to C headers. By grouping them, overlapping types will be disgarded from the header file
    local header=generate_c_header(definedStructures)
  
    script.insertText(0,string.format([[

{$c}

//header created by PREPARECHEADER lines 

%s





{$asm}

]],header))

    --showMessage(script.text)  
  end
  
end

registerAutoAssemblerCommand("PREPARECHEADER", function() end)  --highlight but don't do anything else
registerAutoAssemblerPrologue(PREPARECHEADER)