From 5b8e1cd5d1d23aa3346d588d33d99d552092bd88 Mon Sep 17 00:00:00 2001 From: Brandon Bosman Date: Mon, 22 Jul 2024 17:04:33 -0400 Subject: [PATCH] Created a minimal example to show related problems It shows that user generated names are not currently prevented from shadowing previously declared variables, whether they were generated by GOOL or previously declared by the user. --- code/drasil-code/package.yaml | 1 + code/drasil-code/test/Main.hs | 5 +-- code/drasil-code/test/NameGenExample.hs | 29 ++++++++++++++++ code/hie.yaml | 3 ++ .../gooltest/cpp/NameGenExample/Makefile | 25 ++++++++++++++ .../cpp/NameGenExample/NameGenExample.cpp | 33 +++++++++++++++++++ .../cpp/NameGenExample/NameGenExample.hpp | 10 ++++++ .../gooltest/csharp/NameGenExample/Makefile | 28 ++++++++++++++++ .../csharp/NameGenExample/NameGenExample.cs | 31 +++++++++++++++++ .../gooltest/java/NameGenExample/Makefile | 13 ++++++++ .../NameGenExample/NameGenExample.java | 33 +++++++++++++++++++ .../gooltest/python/NameGenExample/Makefile | 10 ++++++ .../python/NameGenExample/NameGenExample.py | 14 ++++++++ .../gooltest/swift/NameGenExample/Makefile | 25 ++++++++++++++ .../gooltest/swift/NameGenExample/main.swift | 15 +++++++++ 15 files changed, 273 insertions(+), 2 deletions(-) create mode 100644 code/drasil-code/test/NameGenExample.hs create mode 100644 code/stable/gooltest/cpp/NameGenExample/Makefile create mode 100644 code/stable/gooltest/cpp/NameGenExample/NameGenExample.cpp create mode 100644 code/stable/gooltest/cpp/NameGenExample/NameGenExample.hpp create mode 100644 code/stable/gooltest/csharp/NameGenExample/Makefile create mode 100644 code/stable/gooltest/csharp/NameGenExample/NameGenExample.cs create mode 100644 code/stable/gooltest/java/NameGenExample/Makefile create mode 100644 code/stable/gooltest/java/NameGenExample/NameGenExample/NameGenExample.java create mode 100644 code/stable/gooltest/python/NameGenExample/Makefile create mode 100644 code/stable/gooltest/python/NameGenExample/NameGenExample.py create mode 100644 code/stable/gooltest/swift/NameGenExample/Makefile create mode 100644 code/stable/gooltest/swift/NameGenExample/main.swift diff --git a/code/drasil-code/package.yaml b/code/drasil-code/package.yaml index 5bacd919e3..9057a66601 100644 --- a/code/drasil-code/package.yaml +++ b/code/drasil-code/package.yaml @@ -102,6 +102,7 @@ executables: - PatternTest - VectorTest - NameGenTest + - NameGenExample ghc-options: - -threaded - -O2 diff --git a/code/drasil-code/test/Main.hs b/code/drasil-code/test/Main.hs index 28e26e6dd6..58c6157c6d 100644 --- a/code/drasil-code/test/Main.hs +++ b/code/drasil-code/test/Main.hs @@ -18,6 +18,7 @@ import PatternTest (patternTest) import FileTests (fileTests) import VectorTest (vectorTest) import NameGenTest (nameGenTest) +import NameGenExample (nameGenExample) -- | Renders five GOOL tests (FileTests, HelloWorld, PatternTest, VectorTest, and NameGenTest) -- in Java, Python, C#, C++, and Swift. @@ -60,8 +61,8 @@ classes unRepr unRepr' = zipWith (\p gs -> let (p',gs') = runState p gs pd = unRepr p' in unRepr' $ package pd [makefile [] Program [] gs' pd]) - [helloWorld, patternTest, fileTests, vectorTest, nameGenTest] - (map (unCI . (`evalState` initialState)) [helloWorld, patternTest, fileTests, vectorTest, nameGenTest]) + [helloWorld, patternTest, fileTests, vectorTest, nameGenTest, nameGenExample] + (map (unCI . (`evalState` initialState)) [helloWorld, patternTest, fileTests, vectorTest, nameGenTest, nameGenExample]) -- | Formats code to be rendered. makeCode :: [[FileData]] -> [[AuxData]] -> [(FilePath, Doc)] diff --git a/code/drasil-code/test/NameGenExample.hs b/code/drasil-code/test/NameGenExample.hs new file mode 100644 index 0000000000..634fc024c6 --- /dev/null +++ b/code/drasil-code/test/NameGenExample.hs @@ -0,0 +1,29 @@ +module NameGenExample (nameGenExample) where + +import Drasil.GOOL + +nameGenExample :: OOProg r => GSProgram r +nameGenExample = prog "NameGenExample" "" [fileDoc $ buildModule "NameGenExample" [] [main, helper] []] + +helper :: OOProg r => SMethod r +helper = function "helper" private void [param temp] $ body + [block [listDec 2 result], + listSlice result (valueOf temp) (Just (litInt 1)) (Just (litInt 3)) Nothing] + where + temp = var "temp" (listType int) local + result = var "result" (listType int) local + +main :: OOProg r => SMethod r +main = mainFunction $ body + [block [listDecDef temp [litInt 1, litInt 2, litInt 3], + listDec 2 result], + listSlice result (valueOf temp) (Just (litInt 1)) (Just (litInt 3)) Nothing, + block [ + comment "This shadows a generated name:", + listDecDef temp0 [litInt 1, litInt 2, litInt 3], + comment "This shadows a user-given name:", + listDec 2 result]] + where + temp = mainVar "temp" (listType int) + result = mainVar "result" (listType int) + temp0 = mainVar "temp0" (listType int) diff --git a/code/hie.yaml b/code/hie.yaml index fde371d712..0398cf983e 100644 --- a/code/hie.yaml +++ b/code/hie.yaml @@ -30,6 +30,9 @@ cradle: - path: "drasil-code/test/NameGenTest.hs" component: "drasil-code:exe:codegenTest" + - path: "drasil-code/test/NameGenExample.hs" + component: "drasil-code:exe:codegenTest" + - path: "drasil-codeLang/lib" component: "drasil-codeLang:lib" diff --git a/code/stable/gooltest/cpp/NameGenExample/Makefile b/code/stable/gooltest/cpp/NameGenExample/Makefile new file mode 100644 index 0000000000..ae4dfe4b57 --- /dev/null +++ b/code/stable/gooltest/cpp/NameGenExample/Makefile @@ -0,0 +1,25 @@ +ifeq "$(OS)" "Windows_NT" + TARGET_EXTENSION=.exe +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Linux) + TARGET_EXTENSION= + endif + ifeq ($(UNAME_S), Darwin) + TARGET_EXTENSION= + endif +endif + +# Generated by Drasil v0.1-alpha + +# Project Name: NameGenExample + +build: NameGenExample$(TARGET_EXTENSION) + +NameGenExample$(TARGET_EXTENSION): NameGenExample.hpp NameGenExample.cpp + "$(CXX)" NameGenExample.cpp --std=c++11 -o NameGenExample$(TARGET_EXTENSION) + +run: build + ./NameGenExample$(TARGET_EXTENSION) $(RUNARGS) + +.PHONY: build run diff --git a/code/stable/gooltest/cpp/NameGenExample/NameGenExample.cpp b/code/stable/gooltest/cpp/NameGenExample/NameGenExample.cpp new file mode 100644 index 0000000000..03c1d64aae --- /dev/null +++ b/code/stable/gooltest/cpp/NameGenExample/NameGenExample.cpp @@ -0,0 +1,33 @@ +#include "NameGenExample.hpp" + +#include + +using std::vector; + +int main(int argc, const char *argv[]) { + vector temp{1, 2, 3}; + vector result(2); + + vector temp0(0); + for (int i = 1; i < 3; i++) { + temp0.push_back(temp.at(i)); + } + result = temp0; + + // This shadows a generated name: + vector temp0{1, 2, 3}; + // This shadows a user-given name: + vector result(2); + + return 0; +} + +void helper(vector temp) { + vector result(2); + + vector temp0(0); + for (int i = 1; i < 3; i++) { + temp0.push_back(temp.at(i)); + } + result = temp0; +} diff --git a/code/stable/gooltest/cpp/NameGenExample/NameGenExample.hpp b/code/stable/gooltest/cpp/NameGenExample/NameGenExample.hpp new file mode 100644 index 0000000000..67605f1d04 --- /dev/null +++ b/code/stable/gooltest/cpp/NameGenExample/NameGenExample.hpp @@ -0,0 +1,10 @@ +#ifndef NameGenExample_h +#define NameGenExample_h + +#include + +using std::vector; + +void helper(vector temp); + +#endif diff --git a/code/stable/gooltest/csharp/NameGenExample/Makefile b/code/stable/gooltest/csharp/NameGenExample/Makefile new file mode 100644 index 0000000000..53f0317652 --- /dev/null +++ b/code/stable/gooltest/csharp/NameGenExample/Makefile @@ -0,0 +1,28 @@ +ifeq "$(OS)" "Windows_NT" + TARGET_EXTENSION=.exe + CSC=csc +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Linux) + TARGET_EXTENSION= + CSC=mcs + endif + ifeq ($(UNAME_S), Darwin) + TARGET_EXTENSION= + CSC=mcs + endif +endif + +# Generated by Drasil v0.1-alpha + +# Project Name: NameGenExample + +build: NameGenExample$(TARGET_EXTENSION) + +NameGenExample$(TARGET_EXTENSION): NameGenExample.cs + $(CSC) -out:NameGenExample$(TARGET_EXTENSION) NameGenExample.cs + +run: build + ./NameGenExample$(TARGET_EXTENSION) $(RUNARGS) + +.PHONY: build run diff --git a/code/stable/gooltest/csharp/NameGenExample/NameGenExample.cs b/code/stable/gooltest/csharp/NameGenExample/NameGenExample.cs new file mode 100644 index 0000000000..b2551d545c --- /dev/null +++ b/code/stable/gooltest/csharp/NameGenExample/NameGenExample.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; + +public class NameGenExample { + + public static void Main(string[] args) { + List temp = new List {1, 2, 3}; + List result = new List(2); + + List temp0 = new List(0); + for (int i = 1; i < 3; i++) { + temp0.Add(temp[i]); + } + result = temp0; + + // This shadows a generated name: + List temp0 = new List {1, 2, 3}; + // This shadows a user-given name: + List result = new List(2); + } + + private static void helper(List temp) { + List result = new List(2); + + List temp0 = new List(0); + for (int i = 1; i < 3; i++) { + temp0.Add(temp[i]); + } + result = temp0; + } +} diff --git a/code/stable/gooltest/java/NameGenExample/Makefile b/code/stable/gooltest/java/NameGenExample/Makefile new file mode 100644 index 0000000000..c127915e27 --- /dev/null +++ b/code/stable/gooltest/java/NameGenExample/Makefile @@ -0,0 +1,13 @@ +# Generated by Drasil v0.1-alpha + +# Project Name: NameGenExample + +build: NameGenExample/NameGenExample.class + +NameGenExample/NameGenExample.class: NameGenExample/NameGenExample.java + javac NameGenExample/NameGenExample.java + +run: build + java NameGenExample.NameGenExample $(RUNARGS) + +.PHONY: build run diff --git a/code/stable/gooltest/java/NameGenExample/NameGenExample/NameGenExample.java b/code/stable/gooltest/java/NameGenExample/NameGenExample/NameGenExample.java new file mode 100644 index 0000000000..e84aa0385d --- /dev/null +++ b/code/stable/gooltest/java/NameGenExample/NameGenExample/NameGenExample.java @@ -0,0 +1,33 @@ +package NameGenExample; + +import java.util.ArrayList; +import java.util.Arrays; + +public class NameGenExample { + + public static void main(String[] args) { + ArrayList temp = new ArrayList(Arrays.asList(1, 2, 3)); + ArrayList result = new ArrayList(2); + + ArrayList temp0 = new ArrayList(0); + for (int i = 1; i < 3; i++) { + temp0.add(temp.get(i)); + } + result = temp0; + + // This shadows a generated name: + ArrayList temp0 = new ArrayList(Arrays.asList(1, 2, 3)); + // This shadows a user-given name: + ArrayList result = new ArrayList(2); + } + + private static void helper(ArrayList temp) { + ArrayList result = new ArrayList(2); + + ArrayList temp0 = new ArrayList(0); + for (int i = 1; i < 3; i++) { + temp0.add(temp.get(i)); + } + result = temp0; + } +} diff --git a/code/stable/gooltest/python/NameGenExample/Makefile b/code/stable/gooltest/python/NameGenExample/Makefile new file mode 100644 index 0000000000..4faf2c1f94 --- /dev/null +++ b/code/stable/gooltest/python/NameGenExample/Makefile @@ -0,0 +1,10 @@ +# Generated by Drasil v0.1-alpha + +# Project Name: NameGenExample + +build: + +run: build + python3 NameGenExample.py $(RUNARGS) + +.PHONY: build run diff --git a/code/stable/gooltest/python/NameGenExample/NameGenExample.py b/code/stable/gooltest/python/NameGenExample/NameGenExample.py new file mode 100644 index 0000000000..4ab0f930dd --- /dev/null +++ b/code/stable/gooltest/python/NameGenExample/NameGenExample.py @@ -0,0 +1,14 @@ +def helper(temp): + result = [] + + result = temp[1:3:] + +temp = [1, 2, 3] +result = [] + +result = temp[1:3:] + +# This shadows a generated name: +temp0 = [1, 2, 3] +# This shadows a user-given name: +result = [] diff --git a/code/stable/gooltest/swift/NameGenExample/Makefile b/code/stable/gooltest/swift/NameGenExample/Makefile new file mode 100644 index 0000000000..76c2a6c926 --- /dev/null +++ b/code/stable/gooltest/swift/NameGenExample/Makefile @@ -0,0 +1,25 @@ +ifeq "$(OS)" "Windows_NT" + TARGET_EXTENSION=.exe +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Linux) + TARGET_EXTENSION= + endif + ifeq ($(UNAME_S), Darwin) + TARGET_EXTENSION= + endif +endif + +# Generated by Drasil v0.1-alpha + +# Project Name: NameGenExample + +build: NameGenExample$(TARGET_EXTENSION) + +NameGenExample$(TARGET_EXTENSION): main.swift + swiftc main.swift -o NameGenExample$(TARGET_EXTENSION) + +run: build + ./NameGenExample$(TARGET_EXTENSION) $(RUNARGS) + +.PHONY: build run diff --git a/code/stable/gooltest/swift/NameGenExample/main.swift b/code/stable/gooltest/swift/NameGenExample/main.swift new file mode 100644 index 0000000000..e5393264c5 --- /dev/null +++ b/code/stable/gooltest/swift/NameGenExample/main.swift @@ -0,0 +1,15 @@ +private func helper(_ temp: [Int]) -> Void { + var result: [Int] = [] + + result = [Int](stride(from: 1, to: 3, by: 1)).map({(i: Int) -> Int in temp[i]}) +} + +var temp: [Int] = [1, 2, 3] +var result: [Int] = [] + +result = [Int](stride(from: 1, to: 3, by: 1)).map({(i: Int) -> Int in temp[i]}) + +// This shadows a generated name: +var temp0: [Int] = [1, 2, 3] +// This shadows a user-given name: +var result: [Int] = []