Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add glance feature (draft) #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions BT-lang/Runtime/BTL.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using UnityEngine;
using System; using System.Collections.Generic; using System.Linq;
using UnityEngine; using UnityEditor;
using Elk; using Elk.Basic; using Elk.Basic.Runtime;
using Cx = Elk.Basic.Runtime.Context;
using Record = Elk.Memory.Record;
Expand All @@ -26,6 +25,12 @@ public partial class BTL : MonoBehaviour, LogSource{
[Header("Debugging")]
public bool pauseOnErrors = false;
public bool logErrors = true;
#if UNITY_EDITOR
const float editorLabelHeight = 3.2f;
Color editorLabelColor = Color.black;
string editorLabelText = "Hello";
static Summarizer summarizer = new Summarizer();
#endif
//
Vars _vars;

Expand Down Expand Up @@ -93,6 +98,9 @@ void Update(){
vars.output = interpreter.Run(vars.context)?.ToString();
vars.exceptionMessage = null;
vars.log = vars.context.graph.Format();
var strings = new List<string>();
vars.context.graph.Dump(strings);
editorLabelText = summarizer.AddAll(strings);
if(useHistory) history.Log(vars.log, transform);
vars.context = null;
}catch(Exception ex){
Expand All @@ -104,6 +112,18 @@ void Update(){
}
}

#if UNITY_EDITOR
void OnDrawGizmos(){
GUIStyle style = new GUIStyle();
style.normal.textColor = editorLabelColor;
Handles.Label(
transform.position + Vector3.up * editorLabelHeight,
editorLabelText,
style
);
}
#endif

void OnValidate(){
if(path == null) return;
if(path.EndsWith(".txt")){
Expand Down
50 changes: 50 additions & 0 deletions BT-lang/Runtime/Summarizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;

public class Summarizer{

List<Entry> entries = new List<Entry>();

public string AddAll(List<string> arg){
Entry sel = null;
string selValue = null;
foreach(var str in arg){
if(str.Contains("✗")) continue;
var e = Add(str);
if(sel == null || sel.count > e.count){
sel = e;
selValue = str;
}
}
return selValue;
}


Entry Add(string arg){
arg = Strip(arg);
var e = entries.Find( x => x.value == arg);
if(e == null){
e = new Entry(1, arg);
entries.Add(e);
return e;
}else{
e.count ++;
return e;
}
}

string Strip(string arg){
int i = arg.IndexOf("(");
if(i >= 0) return arg.Substring(0, i);
else return arg;
}

class Entry{
public int count;
public string value;
public Entry(int count, string value){
this.count = count;
this.value = value;
}
}

}
11 changes: 11 additions & 0 deletions BT-lang/Runtime/Summarizer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Elk/Runtime/Basic/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Module Parse(string path, IEnumerable<string> submodules){
src = src.Substring(shebang.Length);
// TODO submodule injection will confuse line numbers
if(submodules != null) foreach(var e in submodules){
//nityEngine.Debug.Log($"Add submodule {e} (ref: {path})");
if(string.IsNullOrEmpty(e)) continue;
src = "with " + e + ";\n" + src;
}
try{
Expand Down
11 changes: 11 additions & 0 deletions Elk/Runtime/Basic/Runtime/CallGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public string Format(){
return @out.ToString();
}

public void Dump(List<string> @out)
=> Dump(@out, root);

void Dump(List<string> @out, Node arg){
@out.Add(arg.info);
if(arg.children == null) return;
foreach(var child in arg.children){
Dump(@out, child);
}
}

void Format(Node node, StringBuilder @out, string spaces){
@out.Append(spaces);
@out.Append(node.info);
Expand Down