Skip to content

Instantly share code, notes, and snippets.

View worldbeater's full-sized avatar
☦️

Artyom V. Gorchakov worldbeater

☦️
View GitHub Profile
@worldbeater
worldbeater / uast.py
Last active April 21, 2024 21:52
Unified Abstract Syntax Tree (UAST). Inspired by https://arxiv.org/abs/2106.09173, created with an intention to implement fuzzy code-to-code search algorithms in a language-agnostic fashion, see "An Approach to Generating Recommendations for Improving the Performance of Software for Heterogeneous Computing Platforms" http://injoit.org/index.php/…
import ast
import pprint
import pycparser
import pycparser.c_ast as cast
# Backus–Naur Form of UAST.
# <body> ::= ('body', <stmt1>, <stmt2>, ...)
# <stmt> ::= ('func', <str>, <params>, <body>)
# | ('if', <expr>, <body>, <body>)
# | ('assign', <expr>, <expr>)
@worldbeater
worldbeater / trs.py
Created April 1, 2024 10:22
Simple term rewriting system (TRS) that is based on structural pattern matching, see https://peps.python.org/pep-0636 and https://inst.eecs.berkeley.edu/~cs294-260/sp24/2024-01-22-term-rewriting
def topdown(rule, expr):
match rule(expr):
case (spec, *args):
return (spec, *(step(rule, arg) for arg in args))
case expr:
return expr
def rewrite(rule, expr):
while (new_expr := topdown(rule, expr)) != expr:
expr = new_expr
@worldbeater
worldbeater / enumerate-and-replace.vba
Last active March 8, 2024 17:40
A Visual Basic for Applications (VBA) code for finding all references (e.g. [42], [2—3, 4]) and increasing their numbers based on the preconfigured N and M values. Useful when inserting a new reference into the beginning of the bibliography of a large DOCX.
Sub Highlight(Phrase As String)
Application.ScreenUpdating = False
With ActiveDocument
With .Range
With .Find
.ClearFormatting
.replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
@worldbeater
worldbeater / П1.py
Last active April 21, 2024 21:58
Sample programs for debugging code-to-code search tools, see https://vestnik.rsreu.ru/ru/archive/2023/vypusk-86/1455-1995-4565-2023-86-96-109
# type: ignore
def query():
array = []
for i in range(10000):
array.append(i * 2)
def tree():
a = 10
nums = [] # A1
@worldbeater
worldbeater / cognitive-complexity.py
Last active April 21, 2024 21:59
A Python library and eDSL for code complexity assessment algorithm synthesis, see our paper "A Rule-Based Algorithm and Its Specializations for Measuring the Complexity of Software in Educational Digital Environments" @ https://www.mdpi.com/2073-431X/13/3/75
import ast
def check(rules, env, node, parent):
total, names = 0, []
for name, (spec, check, weight) in rules.items():
if isinstance(node, spec) and check(node, parent):
if score := weight(env, node) if callable(weight) else weight:
total += score
names.append(name)
return total, '+'.join(names)
@worldbeater
worldbeater / StyleManager.cs
Last active August 1, 2023 21:53
StyleManager helper class, allowing to change Avalonia theme which a Window uses dynamically at runtime. See: https://medium.com/swlh/cross-platform-gui-for-dotnet-applications-bbd284709600
public sealed class StyleManager
{
public enum Theme { Citrus, Sea, Rust, Candy, Magma }
private readonly StyleInclude _magmaStyle = CreateStyle("avares://Citrus.Avalonia/Magma.xaml");
private readonly StyleInclude _candyStyle = CreateStyle("avares://Citrus.Avalonia/Candy.xaml");
private readonly StyleInclude _citrusStyle = CreateStyle("avares://Citrus.Avalonia/Citrus.xaml");
private readonly StyleInclude _rustStyle = CreateStyle("avares://Citrus.Avalonia/Rust.xaml");
private readonly StyleInclude _seaStyle = CreateStyle("avares://Citrus.Avalonia/Sea.xaml");
private readonly Window _window;
public partial class FeedbackView : Form, IViewFor<FeedbackViewModel>
{
public FeedbackView()
{
InitializeComponent();
ViewModel = new FeedbackViewModel(new WinFormsSender(), new Clock());
this.WhenActivated(subscriptions =>
{
this.Bind(ViewModel, x => x.Title, x => x.TitleTextBox.Text)
.DisposeWith(subscriptions);
public class ReactiveFodyViewModel : ReactiveObject
{
private readonly ObservableAsPropertyHelper<string> _greeting;
public string Greeting => _greeting.Value;
[Reactive]
public string Name { get; set; } = string.Empty;
public ReactiveCommand<Unit, Unit> Clear { get; }
public sealed partial class FeedbackView : Page, IViewFor<FeedbackViewModel>
{
public static readonly DependencyProperty ViewModelProperty = DependencyProperty
.Register(nameof(ViewModel), typeof(FeedbackViewModel), typeof(FeedbackView), null);
public FeedbackView()
{
InitializeComponent();
ViewModel = new FeedbackViewModel(new UwpSender(), new Clock());
this.WhenActivated(disposables => { /* handle interactions, etc. */ });
@worldbeater
worldbeater / Commands.fs
Last active March 17, 2019 20:34
Funogram Greetings Bot
module Program
open Funogram.Bot
open Funogram.Api
open ExtCore.Control
open System
/// Handler for '/hello'.
let onHello context =
maybe {