Vipitis commited on
Commit
27e63ab
1 Parent(s): ea1edc9

more tree functions

Browse files
Files changed (2) hide show
  1. app.py +5 -4
  2. utils/tree_utils.py +32 -0
app.py CHANGED
@@ -6,7 +6,7 @@ import numpy as np
6
  import torch
7
  from threading import Thread
8
 
9
- from utils.tree_utils import parse_functions, get_docstrings, grab_before_comments, line_chr2char
10
  from utils.html_utils import make_iframe, construct_embed
11
  PIPE = None
12
 
@@ -235,8 +235,9 @@ def alter_body(old_code, func_id, funcs_list: list, prompt="", temperature=0.2,
235
  func_start_idx = line_chr2char(old_code, func_node.start_point[0], func_node.start_point[1])
236
  identifier_str = func_node.child_by_field_name("type").text.decode() + " " + func_node.child_by_field_name("declarator").text.decode() #func_start_idx:body_start_idx?
237
  body_node = func_node.child_by_field_name("body")
238
- body_start_idx = line_chr2char(old_code, body_node.start_point[0], body_node.start_point[1])
239
- body_end_idx = line_chr2char(old_code, body_node.end_point[0], body_node.end_point[1])
 
240
  print(f"{old_code[body_start_idx:body_end_idx]=}")
241
  model_context = identifier_str # base case
242
  # add any comments at the beginning of the function to the model_context
@@ -267,7 +268,7 @@ def alter_body(old_code, func_id, funcs_list: list, prompt="", temperature=0.2,
267
  altered_code = old_code[:func_start_idx] + model_context + generation + "//the generation didn't complete the function!\n" + old_code[body_end_idx:] #needs a newline to break out of the comment.
268
  return altered_code, pipeline
269
  # raise gr.Error(f"didn't generate a full function: {generation!r}]")
270
- print(f"{first_gened_func=}")
271
  generated_body = first_gened_func.child_by_field_name("body").text.decode()
272
  print(f"{generated_body=}")
273
  altered_code = old_code[:func_start_idx] + identifier_str + generated_body + old_code[body_end_idx:]
 
6
  import torch
7
  from threading import Thread
8
 
9
+ from utils.tree_utils import parse_functions, get_docstrings, grab_before_comments, line_chr2char, node_str_idx
10
  from utils.html_utils import make_iframe, construct_embed
11
  PIPE = None
12
 
 
235
  func_start_idx = line_chr2char(old_code, func_node.start_point[0], func_node.start_point[1])
236
  identifier_str = func_node.child_by_field_name("type").text.decode() + " " + func_node.child_by_field_name("declarator").text.decode() #func_start_idx:body_start_idx?
237
  body_node = func_node.child_by_field_name("body")
238
+ body_start_idx, body_end_idx = node_str_idx(body_node)
239
+ # body_start_idx = line_chr2char(old_code, body_node.start_point[0], body_node.start_point[1])
240
+ # body_end_idx = line_chr2char(old_code, body_node.end_point[0], body_node.end_point[1])
241
  print(f"{old_code[body_start_idx:body_end_idx]=}")
242
  model_context = identifier_str # base case
243
  # add any comments at the beginning of the function to the model_context
 
268
  altered_code = old_code[:func_start_idx] + model_context + generation + "//the generation didn't complete the function!\n" + old_code[body_end_idx:] #needs a newline to break out of the comment.
269
  return altered_code, pipeline
270
  # raise gr.Error(f"didn't generate a full function: {generation!r}]")
271
+ # print(f"{first_gened_func=}")
272
  generated_body = first_gened_func.child_by_field_name("body").text.decode()
273
  print(f"{generated_body=}")
274
  altered_code = old_code[:func_start_idx] + identifier_str + generated_body + old_code[body_end_idx:]
utils/tree_utils.py CHANGED
@@ -6,6 +6,38 @@ GLSL_LANGUAGE = Language('./build/my-languages.so', 'glsl')
6
  parser = Parser()
7
  parser.set_language(GLSL_LANGUAGE)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def parse_functions(in_code):
11
  """
 
6
  parser = Parser()
7
  parser.set_language(GLSL_LANGUAGE)
8
 
9
+ def replace_function(old_func_node, new_func_node):
10
+ """
11
+ replaces the old function node with the new function node
12
+ """
13
+ tree = give_tree(old_func_node)
14
+ old_func_start, old_func_end = node_str_idx(old_func_node)
15
+ # new_func_start, new_func_end = node_str_idx(new_func_node)
16
+ new_code = tree.text[:old_func_start] + new_func_node.text + tree.text[old_func_end:]
17
+ return new_code
18
+
19
+ def get_root(node):
20
+ """
21
+ returns the root node of a node
22
+ """
23
+ while node.parent != None:
24
+ node = node.parent
25
+ return node.parent #still need to call parent here
26
+
27
+ def node_str_idx(node):
28
+ """
29
+ returns the character index of start and end of a node
30
+ """
31
+ whole_text = get_root(node).text.decode()
32
+ start_idx = line_chr2char(whole_text, node.start_point[0], node.start_point[1])
33
+ end_idx = line_chr2char(whole_text, node.end_point[0], node.end_point[1])
34
+ return start_idx, end_idx
35
+
36
+ def give_tree(func_node):
37
+ """
38
+ return the tree where this function node is in
39
+ """
40
+ return parser.parse(func_node.parent.text) #really no better way?
41
 
42
  def parse_functions(in_code):
43
  """