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

Implemented bitwise operators using lua's bit32 library #1

Open
wants to merge 3 commits into
base: master
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
5 changes: 5 additions & 0 deletions Depicofier/ConcreteCombinedLuaListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ public override void EnterNumber([NotNull] CombinedLuaParser.NumberContext conte
public override void EnterOperatorMulDivMod([NotNull] CombinedLuaParser.OperatorMulDivModContext context) {
Parent.EnterOperatorMulDivMod(context);
}

public override void EnterOperatorBitwise([NotNull] CombinedLuaParser.OperatorBitwiseContext context) {
Parent.EnterOperatorBitwise(context);
}

}
}
5 changes: 4 additions & 1 deletion Depicofier/Depicofier.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -59,6 +59,9 @@
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
<None Include="explorers.p8lua">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\P8LuaGrammar\P8LuaGrammar.csproj">
Expand Down
43 changes: 31 additions & 12 deletions Depicofier/Depicofy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,42 @@ private static string Clean(string source, Action<string> log, bool strictMode)
log("Strict mode: " + strictMode);
log("Preprocessing if and print shorthand syntax...");
var processed = Preprocessor.Process(source, strictMode);
var pass1Cleaned = "";
var pass2Cleaned = "";
{
log("Pass 1: Lexing and parsing source...");
var input = new AntlrInputStream(processed);
var lexer = strictMode ? (Lexer)new P8LuaLexer(input) : new CombinedLuaLexer(input);
lexer.RemoveErrorListeners();

log("Lexing and parsing source...");
var input = new AntlrInputStream(processed);
var lexer = strictMode ? (Lexer)new P8LuaLexer(input) : new CombinedLuaLexer(input);
lexer.RemoveErrorListeners();
var tokens = new CommonTokenStream(lexer);
var parser = strictMode ? (ILuaParser)new P8LuaParser(tokens) : new CombinedLuaParser(tokens);
parser.RemoveErrorListeners();

var tokens = new CommonTokenStream(lexer);
var parser = strictMode ? (ILuaParser)new P8LuaParser(tokens) : new CombinedLuaParser(tokens);
parser.RemoveErrorListeners();
log("Pass 1: Processing compound statements...");
var context = parser.Chunk();
var listener = new LuaListener(strictMode ? (ILuaListener)new ConcreteP8LuaListener() : (ILuaListener)new ConcreteCombinedLuaListener(), input, processed);
pass1Cleaned = listener.ReplaceCompoundStatements(context);
}

log("Processing remaining enhanced syntax...");
var context = parser.Chunk();
var listener = new LuaListener(strictMode ? (ILuaListener)new ConcreteP8LuaListener() : (ILuaListener)new ConcreteCombinedLuaListener(), input, processed);
var cleaned = listener.ReplaceAll(context);
{
log("Pass 2: Lexing and parsing source...");
var input = new AntlrInputStream(pass1Cleaned);
var lexer = strictMode ? (Lexer)new P8LuaLexer(input) : new CombinedLuaLexer(input);
lexer.RemoveErrorListeners();

var tokens = new CommonTokenStream(lexer);
var parser = strictMode ? (ILuaParser)new P8LuaParser(tokens) : new CombinedLuaParser(tokens);
parser.RemoveErrorListeners();

log("Processing remaining enhanced syntax...");
var context = parser.Chunk();
var listener = new LuaListener(strictMode ? (ILuaListener)new ConcreteP8LuaListener() : (ILuaListener)new ConcreteCombinedLuaListener(), input, pass1Cleaned);
pass2Cleaned = listener.ReplaceRemaining(context);
}

log("Done");
return cleaned;
return pass2Cleaned;
}

public static string Clean(string source, bool strictMode) {
Expand Down
94 changes: 77 additions & 17 deletions Depicofier/LuaListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Depicofier {
public class LuaListener {
private ICharStream input;
private string source;
private bool justCompoundStatements;

public LuaListener(ILuaListener listener, ICharStream input, string source)
: base() {
Expand All @@ -22,11 +23,28 @@ public LuaListener(ILuaListener listener, ICharStream input, string source)
Listener.Parent = this;
}

public string ReplaceAll([NotNull] ParserRuleContext context) {
public string ReplaceCompoundStatements([NotNull] ParserRuleContext context)
{
justCompoundStatements = true;
GetReplacements(context);

var newSource = source;
while (Replacements.Count > 0) {
while (Replacements.Count > 0)
{
var replacement = Replacements.Pop();
newSource = replacement.Replace(newSource);
}
return newSource;
}

public string ReplaceRemaining([NotNull] ParserRuleContext context)
{
justCompoundStatements = false;
GetReplacements(context);

var newSource = source;
while (Replacements.Count > 0)
{
var replacement = Replacements.Pop();
newSource = replacement.Replace(newSource);
}
Expand All @@ -53,21 +71,7 @@ public virtual void EnterCompoundStatement([NotNull] ParserRuleContext context)
var opAssign = GetText(context.children[1]);
var rhs = GetText(context.children[2]);

string op;

switch (opAssign[0]) {
default:
throw new InvalidOperationException(
string.Format("Invalid operator in compound statement '{0}' on line {1}", opAssign, context.start.Line)
);
case '+':
case '-':
case '*':
case '/':
case '%':
op = opAssign.Substring(0, 1);
break;
}
string op = opAssign.Substring(0,opAssign.Length-1);

var node = context.children[2];
while (node.ChildCount == 1) {
Expand All @@ -84,7 +88,59 @@ public virtual void EnterCompoundStatement([NotNull] ParserRuleContext context)
);
}

public virtual void EnterOperatorBitwise([NotNull] ParserRuleContext context)
{
if (justCompoundStatements) return;

ParserRuleContext parent = (ParserRuleContext)context.Parent;
if (parent.ChildCount == 3)
{
ParserRuleContext binOp = (ParserRuleContext)parent.GetChild(1);
string binOpString = GetText(binOp);

string lhs = GetText(parent.GetChild(0));
string rhs = GetText(parent.GetChild(2));
string replacementText = "";

switch (binOpString)
{
case ">>":
replacementText = string.Format("bin32.arshift( {0}, {1} )", lhs, rhs);
break;
case ">>>":
replacementText = string.Format("bin32.rshift( {0}, {1} )", lhs, rhs);
break;
case "<<>":
replacementText = string.Format("bin32.lrotate( {0}, {1} )", lhs, rhs);
break;
case ">><":
replacementText = string.Format("bin32.rrotate( {0}, {1} )", lhs, rhs);
break;
}

if(replacementText != "")
{
//Console.WriteLine("" + parent.start.StartIndex + "-->" +parent.stop.StopIndex);
//Console.WriteLine("Replacing \'" + GetText(parent) + "\' with \'" + replacementText + "\'");
Replacements.Push(
new Replacement(
parent.start.StartIndex,
parent.stop.StopIndex,
replacementText
)
);
}

//Console.WriteLine( GetText(parent.GetChild(0)) + ":" +
// GetText(parent.GetChild(1)) + ":" +
// GetText(parent.GetChild(2))
//);
}
}

public virtual void EnterOperatorComparison([NotNull] ParserRuleContext context) {
if (justCompoundStatements) return;

if (context.GetText() == "!=") {
Replacements.Push(
new Replacement(
Expand All @@ -97,6 +153,8 @@ public virtual void EnterOperatorComparison([NotNull] ParserRuleContext context)
}

public virtual void EnterOperatorMulDivMod([NotNull] ParserRuleContext context) {
if (justCompoundStatements) return;

if (context.GetText() == "\\") {
Replacements.Push(
new Replacement(
Expand All @@ -109,6 +167,8 @@ public virtual void EnterOperatorMulDivMod([NotNull] ParserRuleContext context)
}

public void EnterNumber([NotNull] ParserRuleContext context) {
if (justCompoundStatements) return;

var literal = context.GetText().ToLowerInvariant();
if (literal.StartsWith("0b")) {
var tokens = literal.Substring(2).Split(new char[] { '.' }, StringSplitOptions.None);
Expand Down
1 change: 1 addition & 0 deletions Depicofier/Replacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public string Replace(string source) {
endStr = source.Substring(end, source.Length - end);
}
if (Length > 0) {
//Console.WriteLine("appending \'" + Text + "\'");
str.Append(Text);
}
if (endStr != null) {
Expand Down
8 changes: 6 additions & 2 deletions P8LuaGrammar/CombinedLua.g4
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ fieldsep
;

operatorCompound
: '+=' | '-=' | '/=' | '*=' | '%=';
: '+=' | '-=' | '/=' | '*=' | '%=' | '^=' |
'&=' | '|=' | '^^=' |
'>>=' | '<<=' |
'>>>=' | '<<>=' | '>><='
;

operatorOr
: 'or';
Expand All @@ -229,7 +233,7 @@ operatorMulDivMod
: '*' | '/' | '%' | '\\';

operatorBitwise
: '&' | '|' | '~' | '<<' | '>>';
: '&' | '|' | '~' | '<<' | '>>' | '>>>' | '<<>' | '>><';

operatorUnary
: 'not' | '#' | '-' | '~';
Expand Down
2 changes: 1 addition & 1 deletion P8LuaGrammar/P8LuaGrammar.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Antlr4.CodeGenerator.4.6.6\build\Antlr4.CodeGenerator.props" Condition="Exists('..\packages\Antlr4.CodeGenerator.4.6.6\build\Antlr4.CodeGenerator.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down
Loading